[英]Programmatically adding hibernate interceptor in JpaProperties - Spring
我正在用 spring boot 编写一个库,我需要通过它以编程方式插入一个休眠拦截器(因为我不能在库中使用.properties
)。
我想避免提供我自己的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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.