You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The property sources created via @PropertySource aren't added to the environment in time for them to be considered when component scanning triggers the evaluation of a profile condition. This means that beans discovered via component scanning and annotated with @Profile aren't instantiated.
The following should illustrate the problem:
package test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Configuration
@PropertySource("test.properties")
@ComponentScan
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
System.out.println(context.getBean(MyBean.class));
System.out.println(context.getBean(MyComponentScannedBean.class));
}
@Bean
@Profile("test")
public MyBean myBean() {
return new MyBean();
}
public class MyBean {
}
@Component
@Profile("test")
public static class MyComponentScannedBean {
}
}
spring.profiles.active=test
When run, MyBean is found but MyComponentScannedBean is not.
It's not really well-defined whether a property source declared on a configuration class is applicable to the component scan run triggered by the same configuration class to begin with. It is kind of intended to only rely on the state at the beginning of the configuration class parsing phase at this point.
That said, admittedly, this may be rather unintuitive. I'll see what I can do for 4.1 there.
The differing behaviour between a bean declared via @Bean and a bean found via component scanning certainly wasn't intuitive to me. I'd expected both to work or neither to work.
Good point about the difference between regular @Bean processing and scanned beans. Fixed for 4.1 GA through a reordering of the processing algorithm, along with #16726.
I'm afraid this doesn't suggest itself as a candidate for backporting. Can we simply consider this a limitation with 4.0.x? There are workarounds, after all...
I think it's reasonable to only make the change in 4.1. It's a change in behaviour so it might catch someone out if it were made in a maintenance release.
Andy Wilkinson opened SPR-12111 and commented
The property sources created via
@PropertySource
aren't added to the environment in time for them to be considered when component scanning triggers the evaluation of a profile condition. This means that beans discovered via component scanning and annotated with@Profile
aren't instantiated.The following should illustrate the problem:
When run,
MyBean
is found butMyComponentScannedBean
is not.Affects: 4.0.6
Reference URL: spring-projects/spring-boot#1417
Issue Links:
@PropertySource
resource locations can not resolve properties from prior sources@PropertySource
processing fails)Referenced from: commits 7c60888
The text was updated successfully, but these errors were encountered: