Spring Boot - 环境@Autowired抛出NullPointerException [英] Spring Boot - Environment @Autowired throws NullPointerException
问题描述
我使用 Spring Boot 0.5.0.M5 进行项目设置。
在其中一个配置文件中我试图 @Autowire Environment
但是失败的是 NullPointerException
。
In one of the configuration files I am trying to @Autowire Environment
but that fails with a NullPointerException
.
以下是我目前的情况:
Application.java
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
JpaConfig.java 其中我正在尝试 @Autowire Environment
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.ui.persistence.repository")
public class JpaConfig {
private static final String DATABASE_DRIVER = "db.driver";
private static final String DATABASE_PASSWORD = "db.password";
private static final String DATABASE_URL = "db.url";
private static final String DATABASE_USERNAME = "db.username";
private static final String HIBERNATE_DIALECT = "hibernate.dialect";
private static final String HIBERNATE_SHOW_SQL = "hibernate.show_sql";
private static final String ENTITYMANAGER_PACKAGES_TO_SCAN
= "entitymanager.packages.to.scan";
@Autowired
private Environment env;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty(DATABASE_DRIVER));
dataSource.setUrl(env.getProperty(DATABASE_URL));
dataSource.setUsername(env.getProperty(DATABASE_USERNAME));
dataSource.setPassword(env.getProperty(DATABASE_PASSWORD));
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean
= new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPersistenceProviderClass(
HibernatePersistence.class);
entityManagerFactoryBean.setPackagesToScan(
env.getProperty(ENTITYMANAGER_PACKAGES_TO_SCAN));
entityManagerFactoryBean.setJpaProperties(hibernateProperties());
return entityManagerFactoryBean;
}
}
我正在尝试加载在a中配置的数据库属性属性文件。但是, Environment
未注入,代码因 NullPointerException
而失败。我没有XML文件中的任何配置。
I am trying to load the database properties configured in a properties file. However, the Environment
is not injected and the code fails with NullPointerException
. I do not have any configuration in XML files.
对于属性文件,我已经配置 PropertySourcesPlaceholderConfigurer
这样:
For the properties file I have configured PropertySourcesPlaceholderConfigurer
this way:
@Configuration
@PropertySource("classpath:database.properties")
public class PropertyConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
我尝试过交换 @自动装配
, @Resource
和 @Inject
但到目前为止还没有任何工作。非常感谢任何帮助。谢谢。
I have tried swapping @Autowired
, @Resource
and @Inject
but nothing has worked so far. Would appreciate any help. Thanks.
推荐答案
我相信Spring和 EntityManagerFactory $ c $存在一些生命周期问题c>,你可能已经违反了那些(在4.0.0.RC1中修复) - 如果你的
@Configuration
类被超级实例化,它可能没有资格自动装配。您可以从日志输出中判断是否是这种情况。
I believe there were some lifecycle issues with Spring and the EntityManagerFactory
, and you might have fallen foul of those (fixed in 4.0.0.RC1) - if your @Configuration
class gets instantiated super early, it might not be eligible for autowiring. You can probably tell from the log output if that is the case.
您是否感兴趣,是否知道 JpaConfig提供的功能如果您使用
和 @EnableAutoConfiguration
,那么 PropertyConfig
已预先打开当您 @ComponentScan
您的存储库定义的包时?请参阅 JPA以Spring Boot中的示例为例。
Just out of interest, did you know that the functionality provided by your JpaConfig
and PropertyConfig
is already presetn out of the box if you use @EnableAutoConfiguration
(as long as you @ComponentScan
that package where your repositories are defined)? See the JPA sample in Spring Boot for an example.
这篇关于Spring Boot - 环境@Autowired抛出NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!