복붙노트

[SPRING] Spring + EntityManagerFactory + 최대 절전 모드 청취자 + 삽입

SPRING

Spring + EntityManagerFactory + 최대 절전 모드 청취자 + 삽입

나는 간단한 질문이있다. @Ressource 나 @Autowired를 통해 의존성 주입을 Hibernate EventListener에 추가 할 수 있습니까?

내 entitymanagerfactory 구성을 보여 드리겠습니다 :

<bean id="entityManagerFactory" class="org.hibernate.ejb.EntityManagerFactoryImpl">
    <qualifier value="entityManagerFactory" />
    <constructor-arg>
        <bean
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceUnitManager">
                <bean
                    class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManagerr">
                    <property name="defaultDataSource" ref="dataSource" />
                </bean>
            </property>
            <property name="dataSource" ref="dataSource" />
            <property name="persistenceUnitName" value="mis" />
            <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
            <property name="jpaProperties" ref="jpa.properties" />
            <property name="jpaDialect" ref="jpaDialect" />
            <property name="jpaVendorAdapter">
                <bean
                    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                    <property name="generateDdl" value="true" />
                    <property name="database">
                        <util:constant
                            static-field="org.springframework.orm.jpa.vendor.Database.POSTGRESQL" />
                    </property>
                    <property name="showSql" value="true" />
                </bean>
            </property>

        </bean>
    </constructor-arg>
</bean>

지금은 jpa.properties를 통해 리스너를 등록합니다.

hibernate.ejb.event.load=com.example.hibernate.events.LoadEvent

하지만이 경우에는 청취자에게 봄에 주사가 없습니다. 솔루션을 찾았지만, sessionFactory를 사용하고 entitymanager가 아닌 다른 컨텍스트에서 sessionfactory를 modifiy 할 수 있습니까? 바라건대 누군가이 좋은 아이디어 나 해결책을 가지고이 문제를 해결할 수 있기를 바랍니다.

큰 감사를 드린다!

해결법

  1. ==============================

    1.SessionFactory를 사용했다면 다음과 같은 설정이 될 것입니다 :

    SessionFactory를 사용했다면 다음과 같은 설정이 될 것입니다 :

    <bean id="mySessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- Stripped other stuff -->
        <property name="eventListeners">
            <map>
                <entry key="pre-load">
                    <bean class="com.mycompany.MyCustomHibernateEventListener1" />
                </entry>
                <entry key="pre-persist">
                    <bean class="com.mycompany.MyCustomHibernateEventListener2" />
                </entry>
            </map>
        </property>
    </bean>
    

    하지만 JPA를 사용하고 있기 때문에이 스레드에서 설명한대로 AOP를 사용해야합니다.

    또는 할 수 있습니다.

    기본 클래스 :

    public abstract class ListenerBase{
    
        protected void wireMe(){
            ApplicationContext ctx = ContextHelper.getCurrentApplicationContext();
            ctx.getAutowireCapableBeanFactory().autowireBean(this);
        }
    
    }
    

    이제 lifecycle 메소드에서 wireMe ()를 먼저 호출하십시오.

    최신 정보:

    다음은 ContextHelper의 샘플 구현입니다.

    public final class ContextHelper implements ApplicationContextAware{
    
        private static final ContextHelper INSTANCE = new ContextHelper();
        private ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(final ApplicationContext applicationContext){
            this.applicationContext = applicationContext;
        }
    
        public static ApplicationContext getCurrentApplicationContext(){
            return INSTANCE.applicationContext;
        };
    
        public static ContextHelper getInstance(){
            return INSTANCE;
        }
    
        private ContextHelper(){
        }
    
    }
    

    다음과 같이 Spring Bean 설정에서 연결한다.

    <bean class="com.mycompany.ContextHelper" factory-method="getInstance" />
    
  2. from https://stackoverflow.com/questions/4143881/spring-entitymanagerfactory-hibernate-listeners-injection by cc-by-sa and MIT license