Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore BeanNotOfRequiredTypeException as well as NoSuchBeanDefinitionException #34191

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading