解决方案 »

  1.   

    [code=html]<?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:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 加载配置属性文件 -->
    <context:property-placeholder location="classpath*:/application.properties" />
    <util:properties id="settings" location="/WEB-INF/classes/application.properties" />

    <!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。 -->
    <context:component-scan base-package="com.qianlong.cms">
    <context:exclude-filter type="annotation"
    expression="org.springframework.stereotype.Controller" />
    </context:component-scan> <!-- Process annotations on registered beans like @Autowired... -->
    <context:annotation-config />

    <!-- 国际化的消息资源文件 -->  
        <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
            <property name="basenames">  
                <list>  
                    <!-- 在web环境中一定要定位到classpath 否则默认到当前web应用下找  -->  
                    <value>classpath:messages</value>  
                </list>  
            </property>  
            <property name="defaultEncoding" value="UTF-8"/>  
            <property name="cacheSeconds" value="60"/>  
        </bean>  <!-- 配置数据源 -->
    <bean name="mainDataSource" class="com.alibaba.druid.pool.DruidDataSource"
    init-method="init" destroy-method="close">
    <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->
    <property name="driverClassName" value="${jdbc.driver}" />
        
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" /> <!-- 初始化连接大小 -->
    <property name="initialSize" value="0" />
    <!-- 连接池最大使用连接数量 -->
    <property name="maxActive" value="20" />
    <!-- 连接池最小空闲 -->
    <property name="minIdle" value="0" />
    <!-- 获取连接最大等待时间 -->
    <property name="maxWait" value="60000" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle使用) 
    <property name="poolPreparedStatements" value="true" /> 
    <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> 
    --> <property name="validationQuery" value="SELECT 1" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
    <property name="testWhileIdle" value="true" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
    <property name="minEvictableIdleTimeMillis" value="25200000" /> <!-- 打开removeAbandoned功能 -->
    <property name="removeAbandoned" value="true" />
    <!-- 1800秒,也就是30分钟 -->
    <property name="removeAbandonedTimeout" value="1800" />
    <!-- 关闭abanded连接时输出错误日志 -->
    <property name="logAbandoned" value="true" /> <!-- 监控数据库 -->
    <property name="filters" value="stat" />
    </bean>
    <!-- 配置hibernate session工厂 -->
    <bean id="sessionFactory"  
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
            <property name="dataSource" ref="mainDataSource" />  
            <property name="hibernateProperties">  
                <props>
                 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>  
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>  
                    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>  
                    <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>  
    <!--                 <prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop>  
                    <prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>  
                    <prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>  
                    <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>  
                    <prop key="hibernate.bytecode.use_reflection_optimizer">${hibernate.bytecode.use_reflection_optimizer}</prop>                  <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>  
                    <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>  
                    <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>  
                    <prop key="net.sf.ehcache.configurationResourceName">${net.sf.ehcache.configurationResourceName}</prop>  
                    <prop key="hibernate.cache.use_structured_entries">${hibernate.cache.use_structured_entries}</prop>  --> 
                </props>  
            </property>
    <!--自动扫描注解方式配置的hibernate类文件  -->
    <property name="packagesToScan">
    <list>
    <value>com.qianlong.cms.entity</value>
    </list>
    </property>
    <!-- 自动扫描hbm方式配置的hibernate文件和.hbm文件 -->
    <!-- <property name="mappingDirectoryLocations"> <list> <value>classpath:sy/hbm</value> </list> </property> -->
        </bean> 

    <!-- 开启AOP监听 只对当前配置文件有效 -->  
        <aop:aspectj-autoproxy expose-proxy="true" />
        
        <!-- 注解方式配置事物 -->
    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <!-- 配置事务管理器 -->
    <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- redis support -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
            p:host-name="${redisserver}" p:port="${redisport}"/>
    <!-- redis template definition -->
      <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory"/>
     
    </beans>[/code]
      

  2.   

    可能是OpenSessionInViewFilter的问题,
    在openSessionInView的配置中加上<init-param>
                <param-name>sessionFactoryBeanName</param-name>
               <param-value>mysessionfactory</param-value>
              <!--mysessionfactory是sessionFactory的id名-->
    </init-param>