Question

I have an issue with connecting environment to my Spring project. In this class

@Configuration
@ComponentScan(basePackages = "my.pack.offer.*")
@PropertySource("classpath:OfferService.properties")
public class PropertiesUtil {
    @Autowired
    private Environment environment;



    @Bean
    public String load(String propertyName)
    {
        return environment.getRequiredProperty(propertyName);
    }
}

environment always is null.

Was it helpful?

Solution

Autowiring happens later than load() is called (for some reason).

A workaround is to implement EnvironmentAware and rely on Spring calling setEnvironment() method:

@Configuration
@ComponentScan(basePackages = "my.pack.offer.*")
@PropertySource("classpath:OfferService.properties")
public class PropertiesUtil implements EnvironmentAware {
    private Environment environment;

    @Override
    public void setEnvironment(final Environment environment) {
        this.environment = environment;
    }

    @Bean
    public String load(String propertyName)
    {
        return environment.getRequiredProperty(propertyName);
    }
}

OTHER TIPS

Change @Autowired for @Resource (from javax.annotation) and make it public e.g.:

@Configuration
@PropertySource("classpath:database.properties")
public class HibernateConfigurer {

    @Resource
    public Environment env;

    @Bean
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("database.driverClassName"));
        dataSource.setUrl(env.getProperty("database.url"));
        dataSource.setUsername(env.getProperty("database.username"));
        dataSource.setPassword(env.getProperty("database.password"));
        dataSource.setValidationQuery(env.getProperty("database.validationQuery"));

        return dataSource;
    }
}

And you must register your configurer class in WebApplicationInitializer this way

AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(ApplicationConfigurer.class); //ApplicationConfigurer imports HibernateConfigurer

It's working for me! You may want to check a test project I made.

I solved the same problem with constructor injection:

@Configuration
@PropertySource("classpath:my.properties")
public class MyConfig {
    private Environment environment;

    public MyConfig(Environment environment) {
        this.environment = environment
    }

    @Bean
    public MyBean myBean() {
        return new MyBean(environment.getRequiredProperty("srv.name"))
    }
}

later, I simplified it to this form (to make properties injected properly):

@Configuration
@PropertySource("classpath:my.properties")
public class MyConfig {
    private String serviceName;

    public MyConfig(Environment ignored) {
        /* No-op */
    }

    @Value("${srv.name}")
    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    @Bean
    public MyBean myBean() {
        return new MyBean(requireNonNull(serviceName)); // NPE without environment in constructor
    }
}

Please put this code inside the class where you are trying to autowire the Environment

@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

It solved my issue. Below I give you my class.

@Configuration
@EnableTransactionManagement
public class DatabaseConfig {   
/**
 * DataSource definition for database connection. Settings are read from the
 * application.properties file (using the env object).
 */
@Bean
public DataSource dataSource() {

    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(env.getProperty("db.driver"));
    dataSource.setUrl(env.getProperty("db.url"));
    dataSource.setUsername(env.getProperty("db.username"));
    dataSource.setPassword(env.getProperty("db.password"));
    return dataSource;
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

  @Autowired
  private Environment env;

}

Clean and easy: It also can be called in @PostConstruct method:

@Autowired
private Environment env;

private String appPropertyValue;

@PostConstruct
private void postConstruct() {
    this.appPropertyValue = env.getProperty("myProperty");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top