簡體   English   中英

以編程方式在 JpaProperties 中添加休眠攔截器 - Spring

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM