PersistenceAnnotationBeanPostProcessor是否会以某种方式影响Environment或@PropertySource?

时间:2016-09-28 23:33:10

标签: java spring spring-mvc jpa

嗨,我是Spring和Jpa Integration的初学者。虽然我试图配置我的数据库连接,详细信息处理程序itp。我偶然发现了春天的奇怪行为。

首先,我有3个配置文件:

1)RootConfig - 包含除控制器之外的所有内容

2)WebConfig - 包含每个受控制器注释的Bean

3)JdbcConfig - 包含与dataSource相关的Beans,此配置由RootConfig使用此注释(@Import(JdbcConfig.class))导入。

RootConfig看起来像这样:

@Configuration
@Import(JdbcConfig.class)
@ComponentScan(basePackages = "app", excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = {EnableWebMvc.class, Controller.class})})
public class RootConfig
{

}

JdbcConfig:

@Configuration
@PropertySource("classpath:db.properties")
@EnableTransactionManagement
public class JdbcConfig
{

    @Resource
    public Environment env;


    @Bean
    public DataSource dataSource()
    {
        System.out.println(env);
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName(env.getProperty("dataSource.driverClassName"));
        ds.setUrl(env.getProperty("dataSource.Url"));
        ds.setUsername(env.getProperty("dataSource.username"));
        ds.setPassword(env.getProperty("dataSource.password"));
        return ds;

    }



    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(DataSource ds, JpaVendorAdapter jpaVendorAdapter)
    {
        LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
        emfb.setDataSource(ds);
        emfb.setJpaVendorAdapter(jpaVendorAdapter);
        emfb.setPackagesToScan("app.model");
        return emfb;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter()
    {
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabase(Database.POSTGRESQL);
        adapter.setShowSql(true);
        adapter.setGenerateDdl(true);
        adapter.setDatabasePlatform(env.getProperty("dataSource.dialect"));

        return adapter;
    }



    @Bean
    public BeanPostProcessor beanPostProcessor()
    {
        return new PersistenceExceptionTranslationPostProcessor();
    }


    @Bean
    public JpaTransactionManager jpaTransactionManager(EntityManagerFactory em) {
        return new JpaTransactionManager(em)}}

此时一切正常,环境字段不是空值并包含所有已定义的属性。当我尝试添加Bean PersistenceAnnotationBeanPostProcessor时出现问题所以当我将此Bean添加到JdbcConfig.class时,Environment字段变为null但是当我添加此RootConfig环境时,再次包含所有需要的值。那么propertySource和这个bean有任何已知的问题吗? PersistenceAnnotationBeanPostProcessor会以某种方式影响@PropertySource或@Autorwired(@Inject / @ Ressource)环境字段吗?是否有任何理由必须在主配置中配置环境,并且无法通过@Import从其他配置导入?

1 个答案:

答案 0 :(得分:0)

我认为您的问题与今年春季问题SPR-8269有关。

您是否可以尝试将PersistenceAnnotationBeanPostProcessor bean定义设置为static?

我也有同样的问题,我就这样解决了。