将审计拦截器连接到弹簧上下文

时间:2014-12-22 17:43:29

标签: java spring hibernate spring-mvc interceptor

我是春天的新手,所以这可能是一个微不足道的问题。但我正在研究现有项目,并尝试在此post

之后添加拦截器

如何将拦截器连接到上下文文件?

我正在获得初始化异常:

exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [jpaAppContext.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection available

这就是我的上下文文件的样子:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
       http://www.springframework.org/schema/data/jpa
       http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--uncommment below line and make sure you have persistence.xml or use packagestoscan instead-->
        <!--<property name="persistenceUnitName" value="spring-jpa" />-->
        <property name="packagesToScan" value="com.foo.bar.domain"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
                  p:showSql="true"
                  p:generateDdl="false"
                  p:database="MYSQL"/>
        </property>
        <property name="jpaPropertyMap">
            <map>
            </map>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
          p:entityManagerFactory-ref="entityManagerFactory"/>
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
          p:driverClassName="@{database.driver}"
          p:url="@{database.url}"
          p:username="@{database.user}"
          p:password="@{database.password}"
          p:maxOpenPreparedStatements="@{database.maxOpenPreparedStatements}"
          p:maxWait="@{database.maxWait}"
          p:poolPreparedStatements="true"/>

    <bean id="transactionManagerWithIsolationSupport" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="order" value="1"/>
        <property name="placeholderPrefix"  value="@{"/>
        <property name="locations">
            <list>
                <value>classpath:myApp.default.properties</value>
                <value>file:///opt/bar/config/myApp.local</value>
            </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
    </bean>
    <context:component-scan base-package="com.foo.bar.repository" />
    <jpa:repositories base-package="com.foo.bar.repository"/>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="dBPropertiesReader" class="com.foo.util.DBPropertiesReader" init-method="init">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath*:mappers/**.xml" />
    </bean>
    <bean id="projectSearchMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.foo.bar.dao.projectsearch.ProjectSearchMapper" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    <bean id="restClient" class="com.foo.util.RestClient" />
</beans>

这是试图添加拦截器的方法(没有成功......):

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.HibernateInterceptor">
    <property name="entityInterceptor">
        <bean class="com.foo.bar.utils.audit.MyAuditInterceptor"/>
    </property>
</bean>

这是我的拦截器:

import org.hibernate.EmptyInterceptor;
import java.io.Serializable;

public class AccountManagerAuditInterceptor extends EmptyInterceptor {
    public Boolean onSave(Object entity,Serializable id,
                          Object[] state,String[] propertyNames,Type[] types)throws CallbackException {
        System.out.println("onSave");
        return false;
    }
    public boolean onFlushDirty(Object entity,Serializable id,
                                Object[] currentState,Object[] previousState,
                                String[] propertyNames,Type[] types)
            throws CallbackException {
        System.out.println("onFlushDirty");
        return false;
    }
    public void onDelete(Object entity, Serializable id,
                         Object[] state, String[] propertyNames,
                         Type[] types) {
        System.out.println("onDelete");

    }
    public void preFlush(Iterator iterator) {
        System.out.println("preFlush");
    }

    public void postFlush(Iterator iterator) {
        System.out.println("postFlush");
    }

}

1 个答案:

答案 0 :(得分:0)

该错误与拦截器无关。取消注释在entityFactoryBean中设置persistentUnitName属性的行,并验证persistence.xml是否与所需属性一起存在。这是一个例子 - https://paste.ee/p/j8hkI