BeanFactoryAnnotationUtils searches on bean implementation classes and meta-annotations as well

Issue: SPR-13819
Issue: SPR-13452
This commit is contained in:
Juergen Hoeller 2015-12-28 23:28:08 +01:00
parent 7d0ebddebe
commit 1d8a3e1f07
6 changed files with 118 additions and 7 deletions

View File

@ -26,6 +26,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.AutowireCandidateQualifier; import org.springframework.beans.factory.support.AutowireCandidateQualifier;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
/** /**
@ -113,6 +114,7 @@ public class BeanFactoryAnnotationUtils {
if (bf.containsBean(beanName)) { if (bf.containsBean(beanName)) {
try { try {
BeanDefinition bd = bf.getMergedBeanDefinition(beanName); BeanDefinition bd = bf.getMergedBeanDefinition(beanName);
// Explicit qualifier metadata on bean definition? (typically in XML definition)
if (bd instanceof AbstractBeanDefinition) { if (bd instanceof AbstractBeanDefinition) {
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd; AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName()); AutowireCandidateQualifier candidate = abd.getQualifier(Qualifier.class.getName());
@ -121,15 +123,24 @@ public class BeanFactoryAnnotationUtils {
return true; return true;
} }
} }
// Corresponding qualifier on factory method? (typically in configuration class)
if (bd instanceof RootBeanDefinition) { if (bd instanceof RootBeanDefinition) {
Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod(); Method factoryMethod = ((RootBeanDefinition) bd).getResolvedFactoryMethod();
if (factoryMethod != null) { if (factoryMethod != null) {
Qualifier targetAnnotation = factoryMethod.getAnnotation(Qualifier.class); Qualifier targetAnnotation = AnnotationUtils.getAnnotation(factoryMethod, Qualifier.class);
if (targetAnnotation != null && qualifier.equals(targetAnnotation.value())) { if (targetAnnotation != null) {
return true; return qualifier.equals(targetAnnotation.value());
} }
} }
} }
// Corresponding qualifier on bean implementation class? (for custom user types)
Class<?> beanType = bf.getType(beanName);
if (beanType != null) {
Qualifier targetAnnotation = AnnotationUtils.getAnnotation(beanType, Qualifier.class);
if (targetAnnotation != null) {
return qualifier.equals(targetAnnotation.value());
}
}
} }
catch (NoSuchBeanDefinitionException ex) { catch (NoSuchBeanDefinitionException ex) {
// Ignore - can't compare qualifiers for a manually registered singleton object // Ignore - can't compare qualifiers for a manually registered singleton object

View File

@ -20,10 +20,10 @@ import java.io.Serializable;
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test; import org.junit.Test;
import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
@ -47,8 +47,16 @@ public class AnnotationDrivenTests {
@Test @Test
public void withConfigurationClass() throws Exception { public void withConfigurationClass() throws Exception {
ApplicationContext parent = new AnnotationConfigApplicationContext(TransactionManagerConfiguration.class);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"annotationDrivenConfigurationClassTests.xml"}, getClass(), parent);
doTestWithMultipleTransactionManagers(context);
}
@Test
public void withAnnotatedTransactionManagers() throws Exception {
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
parent.register(TransactionManagerConfiguration.class); parent.registerBeanDefinition("transactionManager1", new RootBeanDefinition(SynchTransactionManager.class));
parent.registerBeanDefinition("transactionManager2", new RootBeanDefinition(NoSynchTransactionManager.class));
parent.refresh(); parent.refresh();
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"annotationDrivenConfigurationClassTests.xml"}, getClass(), parent); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"annotationDrivenConfigurationClassTests.xml"}, getClass(), parent);
doTestWithMultipleTransactionManagers(context); doTestWithMultipleTransactionManagers(context);

View File

@ -0,0 +1,31 @@
/*
* Copyright 2002-2015 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 java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.springframework.beans.factory.annotation.Qualifier;
/**
* @author Juergen Hoeller
*/
@Qualifier("noSynch")
@Retention(RetentionPolicy.RUNTIME)
public @interface NoSynch {
}

View File

@ -0,0 +1,32 @@
/*
* Copyright 2002-2015 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.tests.transaction.CallCountingTransactionManager;
/**
* @author Juergen Hoeller
*/
@NoSynch
@SuppressWarnings("serial")
public class NoSynchTransactionManager extends CallCountingTransactionManager {
public NoSynchTransactionManager() {
setTransactionSynchronization(CallCountingTransactionManager.SYNCHRONIZATION_NEVER);
}
}

View File

@ -0,0 +1,29 @@
/*
* Copyright 2002-2015 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.annotation.Qualifier;
import org.springframework.tests.transaction.CallCountingTransactionManager;
/**
* @author Juergen Hoeller
*/
@Qualifier("synch")
@SuppressWarnings("serial")
public class SynchTransactionManager extends CallCountingTransactionManager {
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -35,7 +35,7 @@ public class TransactionManagerConfiguration {
} }
@Bean @Bean
@Qualifier("noSynch") @NoSynch
public PlatformTransactionManager transactionManager2() { public PlatformTransactionManager transactionManager2() {
CallCountingTransactionManager tm = new CallCountingTransactionManager(); CallCountingTransactionManager tm = new CallCountingTransactionManager();
tm.setTransactionSynchronization(CallCountingTransactionManager.SYNCHRONIZATION_NEVER); tm.setTransactionSynchronization(CallCountingTransactionManager.SYNCHRONIZATION_NEVER);