Skip to content

Commit

Permalink
Ignore BeanNotOfRequiredTypeException as well as NoSuchBeanDefinition…
Browse files Browse the repository at this point in the history
…Exception

Closes GH-34187
  • Loading branch information
quaff committed Jan 3, 2025
1 parent b6de2b0 commit b678611
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -31,6 +31,7 @@

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils;
Expand Down Expand Up @@ -81,6 +82,7 @@
* @author Mark Paluch
* @author Sebastien Deleuze
* @author Enric Sala
* @author Yanming Zhou
* @since 1.1
* @see PlatformTransactionManager
* @see ReactiveTransactionManager
Expand Down Expand Up @@ -506,7 +508,7 @@ else if (targetClass != null) {
try {
return determineQualifiedTransactionManager(this.beanFactory, typeQualifier);
}
catch (NoSuchBeanDefinitionException ex) {
catch (NoSuchBeanDefinitionException | BeanNotOfRequiredTypeException ex) {
// Consider type qualifier as optional, proceed with regular resolution below.
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
Expand Down Expand Up @@ -58,6 +58,7 @@
* @author Juergen Hoeller
* @author Stephane Nicoll
* @author Sam Brannen
* @author Yanming Zhou
* @since 3.1
*/
class EnableTransactionManagementTests {
Expand Down Expand Up @@ -287,6 +288,34 @@ void gh24291TransactionManagerViaQualifierAnnotation() {
ctx.close();
}

@Test
void transactionManagerViaQualifierAnnotationWithNoSuchBeanDefinitionException() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Gh34187Config.class);
TransactionalTestBeanWithNonExistentQualifier bean = ctx.getBean(TransactionalTestBeanWithNonExistentQualifier.class);
CallCountingTransactionManager txManager = ctx.getBean("txManager", CallCountingTransactionManager.class);

bean.findAllFoos();
assertThat(txManager.begun).isEqualTo(1);
assertThat(txManager.commits).isEqualTo(1);
assertThat(txManager.rollbacks).isEqualTo(0);

ctx.close();
}

@Test
void transactionManagerViaQualifierAnnotationWithBeanNotOfRequiredTypeException() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Gh34187Config.class);
TransactionalTestBeanWithInvalidQualifier bean = ctx.getBean(TransactionalTestBeanWithInvalidQualifier.class);
CallCountingTransactionManager txManager = ctx.getBean("txManager", CallCountingTransactionManager.class);

bean.findAllFoos();
assertThat(txManager.begun).isEqualTo(1);
assertThat(txManager.commits).isEqualTo(1);
assertThat(txManager.rollbacks).isEqualTo(0);

ctx.close();
}

@Test
void spr14322AnnotationOnInterfaceWithInterfaceProxy() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14322ConfigA.class);
Expand Down Expand Up @@ -386,6 +415,17 @@ public void saveQualifiedFooWithAttributeAlias() {
public static class TransactionalTestBeanSubclass extends TransactionalTestBean {
}

@Service
@Qualifier("nonExistentBean")
public static class TransactionalTestBeanWithNonExistentQualifier extends TransactionalTestBean {
}

@Service
@Qualifier("transactionalTestBeanWithInvalidQualifier")
public static class TransactionalTestBeanWithInvalidQualifier extends TransactionalTestBean {
}



@Configuration
static class PlaceholderConfig {
Expand Down Expand Up @@ -601,6 +641,36 @@ public CallCountingTransactionManager otherTxManager() {
}
}

@Configuration
@EnableTransactionManagement
@Import(PlaceholderConfig.class)
static class Gh34187Config {

@Autowired
public void initializeApp(ConfigurableApplicationContext applicationContext) {
applicationContext.getBeanFactory().registerSingleton(
"qualifiedTransactionManager", new CallCountingTransactionManager());
applicationContext.getBeanFactory().registerAlias("qualifiedTransactionManager", "qualified");
}

@Bean
public TransactionalTestBeanWithNonExistentQualifier transactionalTestBeanWithNonExistentQualifier() {
return new TransactionalTestBeanWithNonExistentQualifier();
}

@Bean
public TransactionalTestBeanWithInvalidQualifier transactionalTestBeanWithInvalidQualifier() {
return new TransactionalTestBeanWithInvalidQualifier();
}

@Bean
@Primary
public CallCountingTransactionManager txManager() {
return new CallCountingTransactionManager();
}
}



public interface BaseTransactionalInterface {

Expand Down

0 comments on commit b678611

Please sign in to comment.