EclipseLink Lazy Loading Settings

EclipseLink 預設啟用 lazy,但須透由 LoadTimeWeaver lib 來代理集成才有其效用,本篇是透由 Container 的 loadTimeWeaver 進行配置,以下為啟用範例 (spring-data-jpa + eclipseLink):

  1. spring configuration,必須透由標注啟用LoadTimeWeaving,並實作 LoadTimeWeavingConfigurer interfaces 來配置container 的 loadTimeWeaver:
    @Configuration
    ...
    @EnableLoadTimeWeaving
    public class AppConfig implements LoadTimeWeavingConfigurer {
        // ...
        /** 此Container環境是glassfish, 若為tomcat可用TomcatLoadTimeWeaver */
        @Override
        public LoadTimeWeaver getLoadTimeWeaver() {
            return new GlassFishLoadTimeWeaver();
        }
        // ...
    }
    
  2. spring configuration 增加 loadTimeWeaver Bean,並將 LocalContainerEntityManagerFactoryBean 設定 loadTimeWeaver 屬性:
    ...
    public class AppConfig implements LoadTimeWeavingConfigurer {
        ...
        @Bean
        public GlassFishLoadTimeWeaver loadTimeWeaver() {
            return new GlassFishLoadTimeWeaver();
        }
    
        @Bean
        public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
            LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
            entityManagerFactoryBean.setJtaDataSource(dataSource());
            entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());
            entityManagerFactoryBean.setLoadTimeWeaver(loadTimeWeaver());
            // ...
        } 
        // ...
    }<span data-mce-type="bookmark" style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" class="mce_SELRES_start"></span>
    
  3. 大功告成, enjoy~

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料