Spring Boot - 环境@Autowired 抛出 NullPointerException [英] Spring Boot - Environment @Autowired throws NullPointerException
问题描述
我有一个使用 Spring Boot 0.5.0.M5 的项目设置.
I have a project setup using 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;
}
}
我正在尝试加载在属性文件中配置的数据库属性.但是,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();
}
}
我尝试过交换 @Autowired
、@Resource
和 @Inject
但到目前为止没有任何效果.将不胜感激任何帮助.谢谢.
I have tried swapping @Autowired
, @Resource
and @Inject
but nothing has worked so far. Would appreciate any help. Thanks.
推荐答案
我相信 Spring 和 EntityManagerFactory
存在一些生命周期问题,您可能已经遇到了这些问题(在 4.0.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
和 PropertyConfig
提供的功能如果您使用 @EnableAutoConfiguration
(只要您 @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屋!