简体   繁体   中英

Spring Bean Unable to Locate Configuration Properties Bean

I am writing a test using TestNG and Spring Framework (along with Spring Boot), and am running into the application context being unable to locate a particular configuration properties bean.

The setup involves two separate configurations, both referenced by the test. A simple representation of the setup causing the issue would involve these configurations:

@EnableConfigurationProperties
public class SomePropertyConfiguration {

    @Bean(name = "someConfiguration")
    @ConfigurationProperties(prefix = "test.prefix")
    public CustomPropertyClass customPropertyClass(){
        return new CustomPropertyClass();
    }

}

@Configuration
public class SomeConfiguration {

    public ISomethingApi somethingApi(CustomPropertyClass customPropertyClass){
        return new SomethingApi(customPropertyClass.getProperty());
    }
}

The test is setup in a simple way, just to test a couple base properties:

@TestPropertySource("classpath:/test.properties")
@ContextConfiguration(classes = { SomePropertyConfiguration.class, SomeConfiguration.class })
public class SomethingApiTest extends AbstractTestNGSpringContextTests {

    @Inject
    private ISomethingApi somethingApi;

    @Test
    public void test(){
        // Do stuff
    }
}

The end result of these setups is an exception where the configuration for SomethingApi cannot find a valid bean of type CustomPropertyClass .

It is worth noting that if SomeConfiguration is removed from the test, and CustomPropertyClass is injected into the test directly, everything works and the properties are the expected values, which is what is most perplexing about the issue - so something seems to be causing spring to not deal with order of wiring for beans correctly. I have similar setups in other projects which operate as expected, but I have been unable to find any meaningful differences that might cause this behavior (both projects are on the same major version of Spring and Spring Boot)

EDIT:

I have tried with and without @Configuration on SomePropertyConfiguration , without any change in results. In the other project referenced with this working, the property configuration class does not include that explicit annotation, so I left it off in the main example above. I also tried using an @Autowired field of the property bean in the second configuration and referencing it that way for the dependency, instead of as a parameter (in case that impacted order of wiring or something), which also had no impact on the results.

I am using Spring framework 5.1.7.RELEASE , and Spring Boot 2.0.9.RELEASE

After some additional investigation, I found the following in the logs

a definition for bean 'someConfiguration' already exists. This top-level bean definition is considered as an override

it turns out that in this circumstance, the naming conventions in my project had caused a collision between the name of the property-class bean, and the project's overall configuration bean. Both beans and configuration classes appear to share a namespace, and will not cause a crash/error (directly) if they collide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM