Skip to content

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

Closed
@spring-projects-issues

Description

@spring-projects-issues
Collaborator

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

Activity

spring-projects-issues

spring-projects-issues commented on May 27, 2016

@spring-projects-issues
CollaboratorAuthor

Juergen Hoeller commented

Good catch! We simply did not traverse interface hierarchies there before. Fixed in the upcoming 4.3.0.BUILD-SNAPSHOT now and scheduled for a backport to 4.2.7.

added
status: backportedAn issue that has been backported to maintenance branches
in: coreIssues in core modules (aop, beans, core, context, expression)
on Jan 11, 2019
added this to the 4.3 GA milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @jhoeller@spring-projects-issues

      Issue actions

        @Bean not detected in extended interface [SPR-14288] · Issue #18860 · spring-projects/spring-framework