我正在用spring boot编写一个库,并且我需要以编程方式通过它插入一个休眠拦截器(因为我不能.properties
在lib中使用a )。
我想避免提供自己的sessionFactory
bean,我认为最好将这种可能性留给一个正在实施的项目,同时也可以避免手动扫描实体。
我的愚蠢想法是我可以将拦截器“注入”到JpaProperties
。那根本没有用,它运行了,@PostConstruct
但是什么都没有改变。我感觉这是行不通的,但是我想了解原因,以及如何使它起作用。
@Autowired private JpaProperties properties;
@Autowired private MyInterceptor myInterceptor; //yep a bean
@PostConstruct public void add() {
((Map) properties.getProperties())
.put(
"hibernate.session_factory.interceptor",
myInterceptor
);
}
由于这是使用@PostConstruct
注释,因此JpaProperties
仅在中EntityManagerFactoryBuilder
创建后,才会添加JpaBaseConfiguration
。这意味着在此之后,将不会在构建器中显示对属性映射的更改。
要自定义JpaProperties
,您应该实例化一个将配置添加到其中的bean,例如:
@Primary
@Bean
public JpaProperties jpaProperties() {
JpaProperties properties = new JpaProperties();
properties.getProperties().put("hibernate.session_factory.interceptor", myInterceptor);
return properties;
}
然后将其注入HibernateJpaConfiguration
并在构造时使用EntityManagerFactoryBuilder
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句