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

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

Comments

@spring-projects-issues
Copy link
Collaborator

@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

@Configuration
@PropertySource(value = "classpath:/application.properties", ignoreResourceNotFound = false)
@ComponentScan(basePackages = "com.home.services")
public class ApplicationConfig {

    @Autowired
    public Environment env;

    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

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

    @Bean
    public LocalValidatorFactoryBean validator() {
        LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
        localValidatorFactoryBean.setValidationMessageSource(messageSource());
        return localValidatorFactoryBean;
    }

    @Bean
    public JavaMailSender mailSender() {
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
        javaMailSender.setHost(env.getProperty("smtp.host"));
        javaMailSender.setPort(env.getProperty("smtp.port", Integer.class));
        javaMailSender.setUsername(env.getProperty("smtp.username"));
        javaMailSender.setPassword(env.getProperty("smtp.password"));
        Properties mailProperties = new Properties();
        mailProperties.put("mail.smtp.auth", "true");
        mailProperties.put("mail.smtp.starttls.enable", "true");
        javaMailSender.setJavaMailProperties(mailProperties);
        return javaMailSender;
    }

    @Bean
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(5);
        threadPoolTaskExecutor.setMaxPoolSize(10);
        threadPoolTaskExecutor.setQueueCapacity(25);
        return threadPoolTaskExecutor;
    }

    

}

However, implementation of EnvironmentAware interface solves the problem.


Affects: 4.2.2

@spring-projects-issues
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.

Juergen

Loading

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

Successfully merging a pull request may close this issue.

None yet
2 participants