在JpaProperties中以编程方式添加休眠拦截器-Spring

卢卡斯·诺埃佐德(Lucas Noetzold):

我正在用spring boot编写一个库,并且我需要以编程方式通过它插入一个休眠拦截器(因为我不能.properties在lib中使用a )。

我想避免提供自己的sessionFactorybean,我认为最好将这种可能性留给一个正在实施的项目,同时也可以避免手动扫描实体。

我的愚蠢想法是我可以将拦截器“注入”到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
            );
}
瑞安·加纳(Ryan Garner):

由于这是使用@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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法在Spring Boot 2应用程序中为添加拦截器配置WebMvcConfigurer

Spring异常拦截器

Spring Boot添加Http请求拦截器

如何在Spring Boot中配置拦截器?

在Spring中是否提供了拦截器链接的规定?

Spring拦截器中的URL路径模式匹配

如何在Spring Data Rest中添加自定义拦截器(spring-data-rest-webmvc 2.3.0)

Spring Boot @JmsListener拦截器

Spring MVC拦截器模式

Spring Messaging JMS答复拦截器

Spring拦截器的Java配置,其中拦截器使用自动装配的Spring Bean

在Spring-mvc拦截器中,如何访问处理程序控制器方法?

在Spring Boot中哪里写授权?在弹簧拦截器或过滤器中?

如何在Spring 3的Java配置中连接Hibernate 4拦截器?

Spring-拦截器/过滤器中给定请求的get方法

如何在Spring Boot属性中设置Tomcat的SlowQueryReport拦截器的阈值

Spring MVC中的拦截器和过滤器之间的区别

如何在Spring MVC 3.0中注册处理程序拦截器?

如何在 Spring-Integration 的拦截器中获取消息头

拦截器应位于Spring应用程序中的何处

您如何考虑将业务逻辑放入Spring拦截器/建议中?

我如何使用拦截器在Spring Controller中设置字段

Spring Soap拦截器如何修改消息的内容?

Spring Boot-带命令的打印拦截器

没有XML的Java Spring拦截器

Spring MVC处理程序拦截器无法运行

为什么 Spring 拦截器会被错误的 URL 绕过?

Spring注解@Retryable-如何设置拦截器

Spring拦截器与Servlet过滤器