'Autowired Environment is null
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.
Solution 1:[1]
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.
Solution 2:[2]
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
}
}
Solution 3:[3]
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;
}
Solution 4:[4]
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");
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | ursa |
| Solution 3 | Swathy |
| Solution 4 | Matěj Seifert |
