出现问题的原因:
spring容器作为springmvc的父容器,使用注解配置spring和springmvc的时。如果spring mvc扫描了service和dao层就会将bean纳入自己的管理范围,spring作为父容器是访问不到的,反过来是可以的。一旦出现这样的问题就会导致spring的事务管理失效!
需要注意:
一定要将mvc层与service和dao层分开,所以Controller层的bean管理由SpringMvc管理Service和Dao层由Spring管理。那么在扫描时SparingMcv的配置文件需要格外精确扫描的包,并使用<context:include-filter/>标签限定。
而Spring的配置文件在做组件扫描配置时,要排除Controller层。使用<context:exclude-filter/>标签来排除不需要扫描的组件。
在配置事务处理时添加 int i = 1/0 来进行测试
1.Service层转账逻辑:
//原始代码
@Override
public void transfer(int source, int target, double money) throws MyException {
try {
User sou = userDao.findById(source);
User tar = userDao.findById(target);
sou.setMoney(sou.getMoney() - money);
tar.setMoney(tar.getMoney() + money);
userDao.updateUser(sou);
int i = 1/0;
userDao.updateUser(tar);
}catch (Exception e){
e.printStackTrace();
throw new MyException("转账失败");
}
}
//修改后的代码
@Override
public void transfer(int source, int target, double money) {
User sou = userDao.findById(source);
User tar = userDao.findById(target);
sou.setMoney(sou.getMoney() - money);
tar.setMoney(tar.getMoney() + money);
userDao.updateUser(sou);
int i = 1/0;//出现异常
userDao.updateUser(tar);
}
service层,在转账的操作中int i = 1/0;运行时出现异常,导致客户端页面报错,这样是不友好的,所以,我自定义了异常类,并编写了异常处理器解析器,想友好的处理异常并实现事务的有效控制,但是事与愿违,在service层捕获异常,并抛到Controller层,虽然在客户端看来异常确实友好了,但是事务的控制又失效了。
所以我做了改进,仍保留异常处理机制,将异常的捕获放在了Controller层,问题得到了解决。
2.Controller层的处理代码:
@RequestMapping(path = "/transfer")
public String transfer(@RequestParam("sou") int source,
@RequestParam("tar") int target,
@RequestParam("money") double money,
Model model) throws MyException {
try {
userService.transfer(source,target,money);
model.addAttribute("user",userService.findAll());
return "success";
}catch (Exception e){
e.printStackTrace();
throw new MyException("transfer执行异常");
}
}
自定义的异常处理器解析器:处理自下而生的异常,防止客户端直接报错
@Component
public class ExceptionProcessor implements HandlerExceptionResolver {
private MyException myException;
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) {
if(ex instanceof MyException){
myException = (MyException) ex;
}
ModelAndView mv = new ModelAndView();
mv.addObject("exp",myException.getMessage());
mv.setViewName("error");
return mv;
}
}
SSM整合是配置如下
1.spring的配置文件:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:property-placeholder location="classpath:jdbcConfig.properties"/>
<!--开启注解扫描,希望处理service和dao,controller不需要spring框架处理-->
<context:component-scan base-package="com.kali">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--这个配置完成后sqlMapConfig.xml就可以删除了-->
<!--spring整合Mybatis-->
<!--配置连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!--<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm"/>
<property name="user" value="root"/>
<property name="password" value="super"/>-->
</bean>
<!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>-->
<!--配置SqlSessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置接口所在的包,将为其创建对象-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.kali.dao"/>
</bean>
<!--spring声明式事务-->
<!--<tx:annotation-driven/>-->
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置通知-->
<tx:advice id="conAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置切面-->
<aop:config>
<aop:pointcut id="pt" expression="execution(* com.kali.service.impl.ServiceImpl.*(..))"/>
<aop:advisor advice-ref="conAdvice" pointcut-ref="pt"/>
</aop:config>
<aop:aspectj-autoproxy/>
</beans>
2.spring mvc的配置文件:
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解扫描,只处理Controller-->
<context:component-scan base-package="com.kali.controller"/>
<!--<context:component-scan base-package="com.kali.controller"/>-->
<!--开启spring mvc注解支持-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--过滤静态资源-->
<mvc:resources mapping="/js/**" location="/WEB-INF/js/"/>
<mvc:resources mapping="/css/**" location="/WEB-INF/css/"/>
<mvc:resources mapping="/images/**" location="/WEB-INF/images/"/>
</beans>
3.web.xml配置:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--设置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!--解决中文乱码-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--启动服务器创建Servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>