以下是我的项目配置:
的applicationContext.xml
<context:component-scan base-package="cn.onepiece.module">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!--导入资源文件-->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
<property name="slowSqlMillis" value="10000" />
<property name="logSlowSql" value="false" />
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1"/>
<property name="minIdle" value="1"/>
<property name="maxActive" value="20"/>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!--支持 emoji 4字节 编码-->
<!--<property name="connectionInitSqls" value="set names utf8mb4;"/>-->
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="validationQuery" value="SELECT 'x'"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 不支持mysql 支持 oracle db2.。 -->
<property name="poolPreparedStatements" value="false"/>
<!-- <property name="maxPoolPreparedStatementPerConnectionSize" value="20"
/> -->
<!-- 开启监控 -->
<property name="filters" value="log4j"/>
<property name="proxyFilters">
<list>
<ref bean="stat-filter"/>
</list>
</property>
</bean>
<!--配置sessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
<property name="packagesToScan" value="cn.onepiece.module"/>
</bean>
<!--配置事务-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
为spring-servlet.xml
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<contex:component-scan base-package="cn.onepiece.module">
<contex:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<contex:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</contex:component-scan>
<bean id="velocityViewResolver"
class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.velocity.VelocityLayoutView" />
<property name="layoutUrl" value="layouts/template.vm"></property>
<property name="cache" value="false" />
<property name="prefix" value="templates/" />
<property name="suffix" value=".vm" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="toolboxConfigLocation" value="/WEB-INF/vm/toolbox.xml" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="attributesMap">
<map>
<!--Velocity Escape Tool-->
<entry key="esc"><bean class="org.apache.velocity.tools.generic.EscapeTool"/></entry>
</map>
</property>
</bean>
<bean id="velocityConfig"
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<property name="configLocation">
<value>classpath:velocity.properties</value>
</property>
</bean>
的web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
DAO:
@Repository("memberDao")
public class MemberDaoImpl implements MemberDao {
@Autowired
private SessionFactory sessionFactory;
public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
}
getCurrentSession方法中出现错误
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: createCriteria is not valid without active transaction
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:124)
root cause
org.hibernate.HibernateException: createCriteria is not valid without active transaction
org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
com.sun.proxy.$Proxy44.createCriteria(Unknown Source)
cn.onepiece.module.bbs.member.dao.MemberDaoImpl.findByParam(MemberDaoImpl.java:41)
cn.onepiece.module.bbs.member.service.MemberServiceImpl.weblogin(MemberServiceImpl.java:49)
cn.onepiece.module.bbs.member.controller.MemberController.login_post(MemberController.java:31)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:124)
人们知道这就是为什么?
答案 0 :(得分:0)
<property name="packagesToScan" value="cn.onepiece.module"/>
和<contex:component-scan base-package="cn.onepiece.module">
中的属性applicationContext.xml
和spring-servlet.xml
会导致@Service
带注释的bean的2个实例。
要避免在spring-servlet.xml
中将packagesToScan
属性更改为包含@Controllers
和其他与Web相关的bean的特定包。例如。
<contex:component-scan base-package="cn.onepiece.module.*.*.controller ">
在applicationContext.xml
扫描其余软件包中。不要让扫描的包在这两种情况下重叠。