如何在Spring的applicationContext.xml中注册Hibernate Empty Interceptor

时间:2017-08-23 14:34:28

标签: spring hibernate

如何在Spring应用程序的applicationContext.xml中轻松连接Hibernate EmptyInterceptor(一个审计拦截器)?

不支持任何注释,这是一个较旧的Spring应用程序。

我的Hibernate Empty Interceptor是

public class AuditInterceptor extends EmptyInterceptor {

    private static final long serialVersionUID = 1L;

    @Override
    public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
        // TODO Auto-generated method stub
        System.out.println("onDelete intercepted");
        super.onDelete(entity, id, state, propertyNames, types);
    }

    @Override
    public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
            String[] propertyNames, Type[] types) {
        // TODO Auto-generated method stub
        System.out.println("onFlushDirty intercepted");
        return super.onFlushDirty(entity, id, currentState, previousState, propertyNames, types);
    }

    @Override
    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {       
        // TODO Auto-generated method stub
        System.out.println("onSave intercepted");
        return super.onSave(entity, id, state, propertyNames, types);
    }
}

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">


<beans>

     <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="${jdbc.datasource}" />
 </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
                <prop key="hibernate.c3p0.max_size">10</prop>
                <prop key="hibernate.c3p0.min_size">1</prop>
                <prop key="hibernate.c3p0.timeout">5000</prop>
                <prop key="hibernate.c3p0.max_statements">100</prop>
                <prop key="hibernate.c3p0.idle_test_period">300</prop>
                <prop key="hibernate.c3p0.acquire_increment">2</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>

    </bean>

1 个答案:

答案 0 :(得分:1)

以下工作。谢谢你的提示

<!-- First define the bean -->
<bean id="auditInterceptor" class="mypackage.AuditInterceptor" />

<!-- Now add entityInterceptor property in sessionFactory Bean, referring to this bean -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="entityInterceptor" ref="auditInterceptor" />