Browse Source

BeanFactoryAnnotationUtils consistently applies bean name fallback when no BeanDefinition present

Issue: SPR-11915
(cherry picked from commit f8b6114)
pull/579/head
Juergen Hoeller 12 years ago
parent
commit
b9935e9fe3
  1. 11
      spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationUtils.java
  2. 10
      spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionNamespaceHandlerTests.java
  3. 41
      spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java

11
spring-beans/src/main/java/org/springframework/beans/factory/annotation/BeanFactoryAnnotationUtils.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 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.
@ -34,6 +34,7 @@ import org.springframework.util.ObjectUtils;
* Spring's {@link Qualifier @Qualifier} annotation. * Spring's {@link Qualifier @Qualifier} annotation.
* *
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller
* @since 3.1.2 * @since 3.1.2
* @see BeanFactoryUtils * @see BeanFactoryUtils
*/ */
@ -90,9 +91,13 @@ public class BeanFactoryAnnotationUtils {
if (matchingBean != null) { if (matchingBean != null) {
return matchingBean; return matchingBean;
} }
else if (bf.containsBean(qualifier)) {
// Fallback: target bean at least found by bean name - probably a manually registered singleton.
return bf.getBean(qualifier, beanType);
}
else { else {
throw new NoSuchBeanDefinitionException(qualifier, "No matching " + beanType.getSimpleName() + throw new NoSuchBeanDefinitionException(qualifier, "No matching " + beanType.getSimpleName() +
" bean found for qualifier '" + qualifier + "' - neither qualifier " + "match nor bean name match!"); " bean found for qualifier '" + qualifier + "' - neither qualifier match nor bean name match!");
} }
} }
@ -128,7 +133,7 @@ public class BeanFactoryAnnotationUtils {
} }
} }
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
} }
} }
return false; return false;

10
spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionNamespaceHandlerTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 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.
@ -55,7 +55,7 @@ public class AnnotationTransactionNamespaceHandlerTests extends TestCase {
public void testIsProxy() throws Exception { public void testIsProxy() throws Exception {
TransactionalTestBean bean = getTestBean(); TransactionalTestBean bean = getTestBean();
assertTrue("testBean is not a proxy", AopUtils.isAopProxy(bean)); assertTrue("testBean is not a proxy", AopUtils.isAopProxy(bean));
Map services = this.context.getBeansWithAnnotation(Service.class); Map<String, Object> services = this.context.getBeansWithAnnotation(Service.class);
assertTrue("Stereotype annotation not visible", services.containsKey("testBean")); assertTrue("Stereotype annotation not visible", services.containsKey("testBean"));
} }
@ -110,7 +110,7 @@ public class AnnotationTransactionNamespaceHandlerTests extends TestCase {
public static class TransactionalTestBean { public static class TransactionalTestBean {
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Collection findAllFoos() { public Collection<?> findAllFoos() {
return null; return null;
} }
@ -118,6 +118,10 @@ public class AnnotationTransactionNamespaceHandlerTests extends TestCase {
public void saveFoo() { public void saveFoo() {
} }
@Transactional("qualifiedTransactionManager")
public void saveQualifiedFoo() {
}
@Transactional @Transactional
public void exceptional(Throwable t) throws Throwable { public void exceptional(Throwable t) throws Throwable {
throw t; throw t;

41
spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 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.
@ -21,6 +21,8 @@ import java.util.Map;
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.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AdviceMode; import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -33,10 +35,13 @@ import org.springframework.transaction.annotation.AnnotationTransactionNamespace
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import javax.annotation.PostConstruct;
/** /**
* Tests demonstrating use of @EnableTransactionManagement @Configuration classes. * Tests demonstrating use of @EnableTransactionManagement @Configuration classes.
* *
* @author Chris Beams * @author Chris Beams
* @author Stephane Nicoll
* @since 3.1 * @since 3.1
*/ */
public class EnableTransactionManagementTests { public class EnableTransactionManagementTests {
@ -101,6 +106,21 @@ public class EnableTransactionManagementTests {
} }
} }
@Test
public void spr11915() {
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(Spr11915Config.class);
TransactionalTestBean bean = ctx.getBean(TransactionalTestBean.class);
bean.saveQualifiedFoo();
CallCountingTransactionManager txManager = ctx
.getBean("qualifiedTransactionManager", CallCountingTransactionManager.class);
assertThat(txManager.begun, equalTo(1));
assertThat(txManager.commits, equalTo(1));
assertThat(txManager.rollbacks, equalTo(0));
}
@Configuration @Configuration
@EnableTransactionManagement @EnableTransactionManagement
@ -118,6 +138,25 @@ public class EnableTransactionManagementTests {
static class EnableAspectJTxConfig { static class EnableAspectJTxConfig {
} }
@Configuration
@EnableTransactionManagement
static class Spr11915Config {
@Autowired
private ConfigurableApplicationContext applicationContext;
@PostConstruct
public void initializeApp() {
applicationContext.getBeanFactory().registerSingleton(
"qualifiedTransactionManager", new CallCountingTransactionManager());
}
@Bean
public TransactionalTestBean testBean() {
return new TransactionalTestBean();
}
}
@Configuration @Configuration
static class TxManagerConfig { static class TxManagerConfig {

Loading…
Cancel
Save