Java 自动装配环境为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19421092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Autowired Environment is null
提问by LeYar
I have an issue with connecting environment to my Spring project. In this class
我在将环境连接到我的 Spring 项目时遇到问题。在这个班
@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.
环境始终为空。
采纳答案by Alex Shesterov
Autowiring happens later than load()
is called (for some reason).
自动装配发生得晚于load()
调用(出于某种原因)。
A workaround is to implement EnvironmentAware
and rely on Spring calling setEnvironment()
method:
一种解决方法是实现EnvironmentAware
并依赖 Spring 调用setEnvironment()
方法:
@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);
}
}
回答by Josue Montano
Change @Autowired
for @Resource
(from javax.annotation) and make it public
e.g.:
更改@Autowired
为@Resource
(从javax.annotation中),并使其public
例如:
@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
并且您必须以这种方式在 WebApplicationInitializer 中注册您的配置器类
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.
它对我有用!您可能想查看我制作的一个测试项目。
回答by Swathy
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;
}