java – Spring引导 – 环境@Autowired抛出NullPointerException

前端之家收集整理的这篇文章主要介绍了java – Spring引导 – 环境@Autowired抛出NullPointerException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个使用 Spring Boot 0.5.0.M5的项目设置.

在我的一个配置文件我试图@Autowire环境,但失败与NullPointerException.

这是我到目前为止

Application.java

@EnableAutoConfiguration
@Configuration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

JpaConfig.java在哪里我试图@Autowire环境

@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;
    }
}

我正在尝试加载属性文件中配置的数据库属性.但是,环境不会被注入,并且代码与NullPointerException失败.我在XML文件中没有任何配置.

对于属性文件,我以这种方式配置了PropertySourcesPlaceholderConfigurer:

@Configuration
@PropertySource("classpath:database.properties")
public class PropertyConfig {
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

我已经尝试交换@Autowired,@Resource和@Inject,但迄今没有任何工作.会感谢任何帮助.谢谢.

解决方法

我相信Spring和EntityManagerFactory有一些生命周期的问题,你可能已经有这些错误(在4.0.0.RC1中修复了) – 如果你的@Configuration类被提前超时,可能不符合自动装配的条件.如果是这样,你可以从日志输出中得知.

只是出于兴趣,您是否知道如果您使用@EnableAutoConfiguration(只要您的@ComponentScan该包在哪里定义您的存储库),您的JpaConfig和PropertyConfig提供的功能已经预先设置为开箱即用?以JPA sample in Spring Boot为例.

原文链接:https://www.f2er.com/java/121734.html

猜你在找的Java相关文章