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

Using @Autowired Environment and @Bean PropertyPlaceholderConfigurer in same java-based configuration results to environment == null [SPR-13683] #18258

spring-projects-issues opened this issue Nov 13, 2015 · 1 comment


Copy link

@spring-projects-issues spring-projects-issues commented Nov 13, 2015

Andrey Karandey opened SPR-13683 and commented

Using @Autowired Environment and @Bean PropertyPlaceholderConfigurer in same java-based configuration results to environment == null

@PropertySource(value = "classpath:/", ignoreResourceNotFound = false)
@ComponentScan(basePackages = "")
public class ApplicationConfig {

    public Environment env;

    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();

    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasenames("i18n/messages", "i18n/errors");
        return messageSource;

    public LocalValidatorFactoryBean validator() {
        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        return localValidatorFactoryBean;

    public JavaMailSender mailSender() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setPort(env.getProperty("smtp.port", Integer.class));
        Properties mailProperties = new Properties();
        mailProperties.put("mail.smtp.auth", "true");
        mailProperties.put("mail.smtp.starttls.enable", "true");
        return javaMailSender;

    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        return threadPoolTaskExecutor;



However, implementation of EnvironmentAware interface solves the problem.

Affects: 4.2.2

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Nov 13, 2015

Juergen Hoeller commented

I'm afraid this is a known limitation for configuration classes that expose BeanFactoryPostProcessor beans which are needed very early in the lifecycle, even before the annotation post-processors.

The typical solution is to declare your BeanFactoryPostProcessor beans as static, implying that the configuration class doesn't have to be instantiated for those beans yet, in order for it to be instantiated at the usual time then.



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants