Remove "Feature" support introduced in 3.1 M1

Feature-related support such as @Feature, @FeatureConfiguration,
and FeatureSpecification types will be replaced by framework-provided
@Configuration classes and convenience annotations such as
@ComponentScan (already exists), @EnableAsync, @EnableScheduling,
@EnableTransactionManagement and others.

Issue: SPR-8012,SPR-8034,SPR-8039,SPR-8188,SPR-8206,SPR-8223,
SPR-8225,SPR-8226,SPR-8227
This commit is contained in:
Chris Beams
2011-05-06 19:03:52 +00:00
parent 0a790c143f
commit 111fb71fe1
80 changed files with 857 additions and 6737 deletions

View File

@@ -16,7 +16,15 @@
package org.springframework.transaction.config;
import org.w3c.dom.Element;
import org.springframework.aop.config.AopNamespaceUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.parsing.CompositeComponentDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.context.config.AbstractSpecificationBeanDefinitionParser;
import org.springframework.context.config.FeatureSpecification;
@@ -40,7 +48,7 @@ import org.w3c.dom.Element;
* @since 2.0
* @see TxAnnotationDriven
*/
class AnnotationDrivenBeanDefinitionParser extends AbstractSpecificationBeanDefinitionParser {
class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
/**
* The bean name of the internally managed transaction advisor (mode="proxy").

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,7 +31,7 @@ import org.springframework.util.ClassUtils;
* @author Christian Dupuis
* @since 2.5
*/
class JtaTransactionManagerBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
public class JtaTransactionManagerBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
private static final String WEBLOGIC_JTA_TRANSACTION_MANAGER_CLASS_NAME =
"org.springframework.transaction.jta.WebLogicJtaTransactionManager";
@@ -74,7 +74,7 @@ class JtaTransactionManagerBeanDefinitionParser extends AbstractSingleBeanDefini
@Override
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) {
return TxAnnotationDriven.DEFAULT_TRANSACTION_MANAGER_BEAN_NAME;
return TxNamespaceHandler.DEFAULT_TRANSACTION_MANAGER_BEAN_NAME;
}
}

View File

@@ -48,8 +48,6 @@ import org.springframework.util.xml.DomUtils;
*/
class TxAdviceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
private static final String TRANSACTION_MANAGER_ATTRIBUTE = "transaction-manager";
private static final String METHOD_ELEMENT = "method";
private static final String METHOD_NAME_ATTRIBUTE = "name";
@@ -76,7 +74,7 @@ class TxAdviceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
builder.addPropertyReference("transactionManager", element.getAttribute(TRANSACTION_MANAGER_ATTRIBUTE));
builder.addPropertyReference("transactionManager", TxNamespaceHandler.getTransactionManagerName(element));
List<Element> txAttributes = DomUtils.getChildElementsByTagName(element, ATTRIBUTES_ELEMENT);
if (txAttributes.size() > 1) {

View File

@@ -1,198 +0,0 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.transaction.config;
import org.springframework.beans.factory.parsing.ProblemCollector;
import org.springframework.context.config.AbstractFeatureSpecification;
import org.springframework.context.config.AdviceMode;
import org.springframework.context.config.FeatureSpecificationExecutor;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* TODO SPR-7420: document
*
* @author Chris Beams
* @since 3.1
*/
public final class TxAnnotationDriven extends AbstractFeatureSpecification {
static final String DEFAULT_TRANSACTION_MANAGER_BEAN_NAME = "transactionManager";
private static final Class<? extends FeatureSpecificationExecutor> EXECUTOR_TYPE = TxAnnotationDrivenExecutor.class;
private Object txManager = null;
private Object order = null;
private Boolean proxyTargetClass = false;
private Object mode = AdviceMode.PROXY;
/**
* Create a {@code TxAnnotationDriven} specification assumes the presence of a
* {@link PlatformTransactionManager} bean named {@value #DEFAULT_TRANSACTION_MANAGER_BEAN_NAME}.
*
* <p>See the alternate constructors defined here if your transaction manager does
* not follow this default naming or you wish to refer to it by bean instance rather
* than by bean name.
* @see #TxAnnotationDriven(String)
* @see #TxAnnotationDriven(PlatformTransactionManager)
*/
public TxAnnotationDriven() {
this(DEFAULT_TRANSACTION_MANAGER_BEAN_NAME);
}
/**
* Create a new {@code TxAnnotationDriven} specification that will use the specified
* transaction manager bean name.
*
* @param txManagerBeanName name of {@link PlatformTransactionManager} bean or a
* ${placeholder} or SpEL #{expression} resolving to bean name. If {@code null},
* falls back to default value of {@value #DEFAULT_TRANSACTION_MANAGER_BEAN_NAME}.
*/
public TxAnnotationDriven(String txManagerBeanName) {
super(EXECUTOR_TYPE);
this.txManager = txManagerBeanName != null ?
txManagerBeanName :
DEFAULT_TRANSACTION_MANAGER_BEAN_NAME;
}
/**
* Create a new TxAnnotationDriven specification that will use the specified transaction
* manager.
*
* @param txManager the {@link PlatformTransactionManager} bean to use. Must not be {@code null}.
*/
public TxAnnotationDriven(PlatformTransactionManager txManager) {
super(EXECUTOR_TYPE);
Assert.notNull(txManager, "transaction manager must not be null");
this.txManager = txManager;
}
/**
* Return the transaction manager to use. May be a {@link PlatformTransactionManager}
* instance or a String representing the bean name or a placeholder or SpEL expression
* that resolves to the bean name.
*/
Object transactionManager() {
return this.txManager;
}
/**
* Indicate how transactional advice should be applied.
* @see AdviceMode
*/
public TxAnnotationDriven mode(AdviceMode mode) {
this.mode = mode;
return this;
}
/**
* Indicate how transactional advice should be applied.
* @param name matching one of the labels in the AdviceMode enum;
* placeholder and SpEL expressions are not allowed.
* @see AdviceMode
*/
TxAnnotationDriven mode(String mode) {
if (StringUtils.hasText(mode)) {
this.mode = mode;
}
return this;
}
/**
* Return how transactional advice should be applied.
*/
AdviceMode mode() {
if (this.mode instanceof AdviceMode) {
return (AdviceMode)this.mode;
}
if (this.mode instanceof String) {
return ObjectUtils.caseInsensitiveValueOf(AdviceMode.values(), (String)this.mode);
}
// TODO SPR-7420: deal with in validate & raise problem
throw new IllegalStateException(
"invalid type for field 'mode' (must be of type AdviceMode or String): "
+ this.mode.getClass().getName());
}
/**
* Indicate whether class-based (CGLIB) proxies are to be created as opposed
* to standard Java interface-based proxies.
*
* <p>Note: Class-based proxies require the {@link Transactional @Transactional}
* annotation to be defined on the concrete class. Annotations in interfaces will
* not work in that case (they will rather only work with interface-based proxies)!
*/
public TxAnnotationDriven proxyTargetClass(Boolean proxyTargetClass) {
this.proxyTargetClass = proxyTargetClass;
return this;
}
/**
* Return whether class-based (CGLIB) proxies are to be created as opposed
* to standard Java interface-based proxies.
*/
Boolean proxyTargetClass() {
return this.proxyTargetClass;
}
/**
* Indicate the ordering of the execution of the transaction advisor
* when multiple advice executes at a specific joinpoint. The default is
* {@code null}, indicating that default ordering should be used.
*/
public TxAnnotationDriven order(int order) {
this.order = order;
return this;
}
/**
* Indicate the ordering of the execution of the transaction advisor
* when multiple advice executes at a specific joinpoint. The default is
* {@code null}, indicating that default ordering should be used.
*/
public TxAnnotationDriven order(String order) {
if (StringUtils.hasText(order)) {
this.order = order;
}
return this;
}
/**
* Return the ordering of the execution of the transaction advisor
* when multiple advice executes at a specific joinpoint. May return
* {@code null}, indicating that default ordering should be used.
*/
Object order() {
return this.order;
}
@Override
protected void doValidate(ProblemCollector problems) {
if (this.mode instanceof String) {
if (!ObjectUtils.containsConstant(AdviceMode.values(), (String)this.mode)) {
problems.error("no such mode name: " + this.mode);
}
}
}
}

View File

@@ -1,139 +0,0 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.transaction.config;
import org.springframework.aop.config.AopNamespaceUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
import org.springframework.beans.factory.parsing.ComponentRegistrar;
import org.springframework.beans.factory.parsing.CompositeComponentDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.config.AbstractSpecificationExecutor;
import org.springframework.context.config.SpecificationContext;
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
import org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import org.springframework.util.Assert;
/**
* TODO SPR-7420: document
*
* @author Chris Beams
* @since 3.1
*/
final class TxAnnotationDrivenExecutor extends AbstractSpecificationExecutor<TxAnnotationDriven> {
/**
* The bean name of the internally managed transaction advisor (used when mode == PROXY).
*/
public static final String TRANSACTION_ADVISOR_BEAN_NAME =
"org.springframework.transaction.config.internalTransactionAdvisor";
/**
* The bean name of the internally managed transaction aspect (used when mode == ASPECTJ).
*/
public static final String TRANSACTION_ASPECT_BEAN_NAME =
"org.springframework.transaction.config.internalTransactionAspect";
private static final String TRANSACTION_ASPECT_CLASS_NAME =
"org.springframework.transaction.aspectj.AnnotationTransactionAspect";
@Override
protected void doExecute(TxAnnotationDriven txSpec, SpecificationContext specificationContext) {
BeanDefinitionRegistry registry = specificationContext.getRegistry();
ComponentRegistrar registrar = specificationContext.getRegistrar();
switch (txSpec.mode()) {
case ASPECTJ:
registerTransactionAspect(txSpec, registry, registrar);
break;
case PROXY:
AopAutoProxyConfigurer.configureAutoProxyCreator(txSpec, registry, registrar);
break;
default:
throw new IllegalArgumentException(
String.format("AdviceMode %s is not supported", txSpec.mode()));
}
}
private void registerTransactionAspect(TxAnnotationDriven spec, BeanDefinitionRegistry registry, ComponentRegistrar registrar) {
if (!registry.containsBeanDefinition(TRANSACTION_ASPECT_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition();
def.setBeanClassName(TRANSACTION_ASPECT_CLASS_NAME);
def.setFactoryMethodName("aspectOf");
registerTransactionManager(spec, def);
registrar.registerBeanComponent(new BeanComponentDefinition(def, TRANSACTION_ASPECT_BEAN_NAME));
}
}
private static void registerTransactionManager(TxAnnotationDriven spec, BeanDefinition def) {
Object txManager = spec.transactionManager();
Assert.notNull(txManager, "transactionManager must be specified");
if (txManager instanceof String) {
def.getPropertyValues().add("transactionManagerBeanName", txManager);
} else {
def.getPropertyValues().add("transactionManager", txManager);
}
}
/**
* Inner class to just introduce an AOP framework dependency when actually in proxy mode.
*/
private static class AopAutoProxyConfigurer {
public static void configureAutoProxyCreator(TxAnnotationDriven txSpec, BeanDefinitionRegistry registry, ComponentRegistrar registrar) {
Object source = txSpec.source();
AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(registry, registrar, source, txSpec.proxyTargetClass());
if (!registry.containsBeanDefinition(TRANSACTION_ADVISOR_BEAN_NAME)) {
// Create the TransactionAttributeSource definition.
RootBeanDefinition sourceDef = new RootBeanDefinition(AnnotationTransactionAttributeSource.class);
sourceDef.setSource(source);
sourceDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
String sourceName = registrar.registerWithGeneratedName(sourceDef);
// Create the TransactionInterceptor definition.
RootBeanDefinition interceptorDef = new RootBeanDefinition(TransactionInterceptor.class);
interceptorDef.setSource(source);
interceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
registerTransactionManager(txSpec, interceptorDef);
interceptorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
String interceptorName = registrar.registerWithGeneratedName(interceptorDef);
// Create the TransactionAttributeSourceAdvisor definition.
RootBeanDefinition advisorDef = new RootBeanDefinition(BeanFactoryTransactionAttributeSourceAdvisor.class);
advisorDef.setSource(source);
advisorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
advisorDef.getPropertyValues().add("transactionAttributeSource", new RuntimeBeanReference(sourceName));
advisorDef.getPropertyValues().add("adviceBeanName", interceptorName);
if (txSpec.order() != null) {
advisorDef.getPropertyValues().add("order", txSpec.order());
}
registry.registerBeanDefinition(TRANSACTION_ADVISOR_BEAN_NAME, advisorDef);
CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(txSpec.sourceName(), source);
compositeDef.addNestedComponent(new BeanComponentDefinition(sourceDef, sourceName));
compositeDef.addNestedComponent(new BeanComponentDefinition(interceptorDef, interceptorName));
compositeDef.addNestedComponent(new BeanComponentDefinition(advisorDef, TRANSACTION_ADVISOR_BEAN_NAME));
registrar.registerComponent(compositeDef);
}
}
}
}

View File

@@ -39,6 +39,17 @@ import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
*/
public class TxNamespaceHandler extends NamespaceHandlerSupport {
static final String TRANSACTION_MANAGER_ATTRIBUTE = "transaction-manager";
static final String DEFAULT_TRANSACTION_MANAGER_BEAN_NAME = "transactionManager";
static String getTransactionManagerName(Element element) {
return (element.hasAttribute(TRANSACTION_MANAGER_ATTRIBUTE) ?
element.getAttribute(TRANSACTION_MANAGER_ATTRIBUTE) : DEFAULT_TRANSACTION_MANAGER_BEAN_NAME);
}
public void init() {
registerBeanDefinitionParser("advice", new TxAdviceBeanDefinitionParser());
registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenBeanDefinitionParser());

View File

@@ -1,147 +0,0 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.transaction.annotation;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import java.util.Map;
import org.junit.Test;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.CannotLoadBeanClassException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Feature;
import org.springframework.context.annotation.FeatureConfiguration;
import org.springframework.context.config.AdviceMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.CallCountingTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.AnnotationTransactionNamespaceHandlerTests.TransactionalTestBean;
import org.springframework.transaction.config.TxAnnotationDriven;
/**
* Integration tests for {@link TxAnnotationDriven} support within @Configuration
* classes. Adapted from original tx: namespace tests at
* {@link AnnotationTransactionNamespaceHandlerTests}.
*
* @author Chris Beams
* @since 3.1
*/
public class TxAnnotationDrivenFeatureTests {
@Test
public void transactionProxyIsCreated() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(TxFeature.class, TxManagerConfig.class);
ctx.refresh();
TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
assertThat("testBean is not a proxy", AopUtils.isAopProxy(bean), is(true));
Map<?,?> services = ctx.getBeansWithAnnotation(Service.class);
assertThat("Stereotype annotation not visible", services.containsKey("testBean"), is(true));
}
@Test
public void txManagerIsResolvedOnInvocationOfTransactionalMethod() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(TxFeature.class, TxManagerConfig.class);
ctx.refresh();
TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
// invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
bean.findAllFoos();
}
@Test
public void txManagerIsResolvedCorrectlyWhenMultipleManagersArePresent() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(TxFeature.class, MultiTxManagerConfig.class);
ctx.refresh();
TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
// invoke a transactional method, causing the PlatformTransactionManager bean to be resolved.
bean.findAllFoos();
}
/**
* A cheap test just to prove that in ASPECTJ mode, the AnnotationTransactionAspect does indeed
* get loaded -- or in this case, attempted to be loaded at which point the test fails.
*/
@Test
public void proxyTypeAspectJCausesRegistrationOfAnnotationTransactionAspect() {
try {
new AnnotationConfigApplicationContext(TxWithAspectJFeature.class, TxManagerConfig.class);
fail("should have thrown CNFE when trying to load AnnotationTransactionAspect. " +
"Do you actually have org.springframework.aspects on the classpath?");
} catch (CannotLoadBeanClassException ex) {
ClassNotFoundException cause = (ClassNotFoundException) ex.getCause();
assertThat(cause.getMessage(), equalTo("org.springframework.transaction.aspectj.AnnotationTransactionAspect"));
}
}
}
@FeatureConfiguration
class TxFeature {
@Feature
public TxAnnotationDriven tx(TxManagerConfig txManagerConfig) {
return new TxAnnotationDriven(txManagerConfig.txManager());
}
}
@FeatureConfiguration
class TxWithAspectJFeature {
@Feature
public TxAnnotationDriven tx(PlatformTransactionManager txManager) {
return new TxAnnotationDriven(txManager).mode(AdviceMode.ASPECTJ);
}
}
@Configuration
class TxManagerConfig {
@Bean
public TransactionalTestBean testBean() {
return new TransactionalTestBean();
}
@Bean
public PlatformTransactionManager txManager() {
return new CallCountingTransactionManager();
}
}
@Configuration
class MultiTxManagerConfig extends TxManagerConfig {
@Bean
public PlatformTransactionManager txManager2() {
return new CallCountingTransactionManager();
}
}