Skip to content

@Bean not detected in extended interface [SPR-14288] #18860

Closed
@spring-projects-issues

Description

@spring-projects-issues

Niko Wittenbeck opened SPR-14288 and commented

Hi there,

I'm currently playing with the Spring JavaConf and I'm using lots of interfaces with default methods. It seems that there are issues with @Bean detection in deep inheritance structures.

This config is detected in my test environment:

@Configuration
@Profile("test")
public class SumaSpringTestConfig extends SumaBasicConfig implements SumaTestConfig {
}

This config extends from SumaBasicConfig, which is

public abstract class SumaBasicConfig implements BasicApplicationConfig {
	@Override
	@Bean
	public SumaApplication getApplication() {
		return init(SumaApplication.class);
	}

}

SumaBasicConfig implements the interface BasicApplicationConfig:

public interface BasicApplicationConfig {

	default <T> T init(Class<T> clazz) {
		return ReflectionUtils.newInstanceOf(clazz, true);
	}

	JAFApplication getApplication();

	DataSource dataSource(DataSource jndiDataSource);
}

SumaBasicConfig's SumaApplication extends from JAFApplication, so everything is all right here.

So what still needs to be implemented is dataSource. But SumaSpringTestConfig also implements SumaTestConfig, which is:

public interface SumaTestConfig extends TestConfig {

	@Override
	@Bean
	public default TestTools testTools() {
		return init(SumaTestTools.class);
	}
}

SumaTestConfig extends the TestConfig interface, which is

public interface TestConfig extends BasicApplicationConfig {

	@Override
	@Bean
	public default DataSource dataSource(DataSource jndiDataSource) {
		return ProxyDataSourceBuilder
				.create(jndiDataSource)
				.logQueryBySlf4j("org.hibernate.SQL")
				.asJson()
				.name("dataSource")
				.countQuery()
				.build();
	}

	public TestTools testTools();
}

So this that all requirements are fulfilled and everything is implemented. But when I execute my test, the dataSource bean is not found:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined

When I move my default dataSource implementation from TestConfig to SumaTestConfig, it is recognized properly.

So to me it seems that not all all levels are parsed for the @Bean annotation.


Affects: 4.2.6

Issue Links:

Referenced from: commits 62199e8, 03affa0

Backported to: 4.2.7

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions