【问题标题】:wiring audit interceptor to spring context将审计拦截器连接到 Spring 上下文
【发布时间】:2014-12-22 17:43:29
【问题描述】:

我是 Spring 的新手,所以这可能是一个微不足道的问题。但我正在处理现有项目并尝试在 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");
    }

}

【问题讨论】:

    标签: java spring hibernate spring-mvc interceptor


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2018-08-09
      • 2010-10-27
      • 2015-03-18
      • 2017-03-07
      • 1970-01-01
      • 2014-05-19
      • 2017-02-25
      • 1970-01-01
      • 2011-04-26
      相关资源
      最近更新 更多