问题描述
问题说明:使用Spring+SpringMVC+Hibernate架构来做个练习的时候,就出错了。。。按道理说这个问题,只需要配置一个事务就可以了,但是配置了还是出错。。。求解。。。===========================================================================新增用户出错===========================================================================Web.xml配置文件:<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0"><display-name>springMVC_09_RESTRUL_CRUD</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><!--Spring--><!--Spring配置文件--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:app*.xml</param-value></context-param><!--配置监听器,用于初始化Spring容器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--SpringMVC--><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:springMVC-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><!--处理请求时对字符集编码--><filter><filter-name>CharacterEncodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--HiddenHttpMethodFilter过滤器,可以将POST请求转换成DELETE和PUT请求--><filter><filter-name>HiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class></filter><filter-mapping><filter-name>HiddenHttpMethodFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--配置Spring提供的OpenSessionInViewFilter过滤器,用于解决Hibernate的延迟加载问题!--><filter><filter-name>OpenSessionInViewFilter</filter-name><filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping><filter-name>OpenSessionInViewFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
spring配置文件(applicationContext.xml):<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.1.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.1.xsd"><!--扫描指定包下包含注解的类--><context:component-scanbase-package="cn.t118121.springmvc"><!--唯独不扫描被@Controller注解标注的类--><context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan><beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"><propertyname="driverClassName"value="com.mysql.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost:3306/mydb_01"></property><propertyname="username"value="root"></property><propertyname="password"value="chenzhiyu3360908"></property></bean><beanid="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><propertyname="dataSource"><refbean="dataSource"/></property><propertyname="hibernateProperties"><props><propkey="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><propkey="hibernate.show_sql">true</prop><propkey="hibernate.format_sql">true</prop></props></property><propertyname="mappingDirectoryLocations"value="classpath:cnt118121springmvcbean"/></bean><!--事务管理器--><beanid="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean><!--事务增强--><tx:adviceid="txAdvice"transaction-manager="transactionManager"><tx:attributes><tx:methodname="find*"read-only="true"/><tx:methodname="get*"read-only="true"/><tx:methodname="search*"read-only="true"/><tx:methodname="*"/></tx:attributes></tx:advice><!--定义切面,用于组合事务增强和方法切入点--><aop:config><aop:pointcutexpression="execution(*cn.t118121.springmvc.service.impl.*.*(..))"id="cut1"/><aop:advisoradvice-ref="txAdvice"pointcut-ref="cut1"/></aop:config></beans>
SpringMVC配置文件(springMVC-servlet.xml):<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.1.xsd"><!--扫描指定包下包含注解的类--><context:component-scanbase-package="cn.t118121.springmvc"><!--只扫描被@Controller注解标注的类--><context:include-filtertype="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan><!--配置视图解析器--><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><propertyname="prefix"value="/WEB-INF/jsp/"/><propertyname="suffix"value=".jsp"/></bean></beans>
Controller代码:packagecn.t118121.springmvc.controller;importjavax.annotation.Resource;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importcn.t118121.springmvc.bean.Employee;importcn.t118121.springmvc.service.EmployeeService;@Controller@RequestMapping("/springmvc")publicclassEmployeeController{@ResourceprivateEmployeeServiceemployeeService;/***POST请求,用于获取表单数据,并添加到数据库中,然后重定向到员工列表*/@RequestMapping(value="/employee",method=RequestMethod.POST)publicStringdddEmployee(Employeeemployee){System.out.println(employee);//添加到数据库中employeeService.addEmployee(employee);return"redirect:employeeList";}}
Service代码:packagecn.t118121.springmvc.service.impl;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importcn.t118121.springmvc.bean.Employee;importcn.t118121.springmvc.dao.EmployeeDao;importcn.t118121.springmvc.service.EmployeeService;@Service("employeeService")publicclassEmployeeServiceImplimplementsEmployeeService{@AutowiredprivateEmployeeDaoemployeeDao;@OverridepublicList<Employee>getEmployeeList(){returnemployeeDao.getEmployeeList();}@OverridepublicEmployeegetEmployeeById(Integerid){returnemployeeDao.getEmployeeById(id);}@OverridepublicvoidaddEmployee(Employeeemployee){employeeDao.addEmployee(employee);}@OverridepublicvoiddeleteEmployee(Integerid){employeeDao.deleteEmployee(id);}@OverridepublicvoidupdateEmployee(Employeeemployee){employeeDao.updateEmployee(employee);}}
Dao层的代码省略...麻烦各位帮忙解答下~感激不尽~
解决方案
本帖最后由 chen8643766 于 2015-01-03 11:38:47 编辑
解决方案二:
已解决!原因:重复加载bean了,2个配置文件都加载了一遍bean!SpringMVC配置文件:<!--扫描指定包下包含注解的类--><context:component-scanbase-package="cn.t118121.springmvc"use-default-filters="false"><!--只扫描被@Controller注解标注的类--><context:include-filtertype="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan>
Spring配置文件:<!--扫描指定包下包含注解的类--><context:component-scanbase-package="cn.t118121.springmvc"><!--唯独不扫描被@Controller注解标注的类--><context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Controller"/></context:component-scan>