JAVAEE之Spring学习(三)---通过aop切面实现事务处理

2 篇文章 0 订阅
1 篇文章 0 订阅

需求:通过切面给项目中的service添加事务处理,让数据库表1添加用户失败后回滚,表2无法添加数据

思想:通过aop切面拦截service下的reg()方法,在拦截之前开启事务再进行dao调用,进行事务处理

需要注意的地方:在spring方式下获得到的datasource中的getconnection方法还没有进行拦截,在dao层,是通过queryrunner自动调用getconnection()进行提交,所以要将getconnection()做成单线程形式

service:

package cn.hncu.stud.service;

import java.sql.SQLException;

import cn.hncu.stud.dao.IStudaoa;
import cn.hncu.stud.domain.User;

public class serviceimpl implements IStudentService {
    IStudaoa dao1 = null;
    IStudaoa dao2 = null;

    public IStudaoa getDao1() {
        return dao1;
    }

    public void setDao1(IStudaoa dao1) {
        this.dao1 = dao1;
    }

    public IStudaoa getDao2() {
        return dao2;
    }

    public void setDao2(IStudaoa dao2) {
        this.dao2 = dao2;
    }

    // 注入
    @Override
    public void reg(User user) throws SQLException {
        dao1.reg(user);
        dao2.reg(user);
    }

}

web.xml:将Spring容器放入servletContext中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name></display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            <!-- 如果有路径要用"/"开始 -->
            classpath:beans.xml,
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>StudServlet</servlet-name>
        <servlet-class>cn.hncu.stud.servlet.StudServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>StudServlet</servlet-name>
        <url-pattern>/StudServlet</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

servlet:通过ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());获取spring容器

package cn.hncu.stud.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.hncu.stud.domain.User;
import cn.hncu.stud.service.IStudentService;

public class StudServlet extends HttpServlet {
    private IStudentService service = null;

    @Override
    public void init() throws ServletException {
        // 获取web项目中的spring容器
        ApplicationContext ctx = WebApplicationContextUtils
                .getWebApplicationContext(getServletContext());
        service = ctx.getBean("service", IStudentService.class);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        User user = new User();
        user.setName(name);
        try {
            service.reg(user);
            request.getSession().setAttribute("user", user);
            request.getRequestDispatcher("/jsps/show.jsp").forward(request,
                    response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

applicationContext:用于配置dataSource和拦截getconnection的公共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"

    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        ">
    <bean
        class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
    </bean>
    <!-- 切面=切点+通知 -->
    <bean id="advisor"
        class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">
        <!-- 切点 -->
        <property name="expression">
            <value>
                execution( void cn.hncu.stud..serviceimpl.reg(..) )
            </value>
        </property>
        <property name="advice">
            <bean class="cn.hncu.stud.utils.AroundAdvice">
                <property name="data" ref="dataSource"></property>
            </bean>
        </property>
    </bean>


    <!-- 切面=切点+通知 -->
    <bean id="advisor1"
        class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">
        <!-- 切点 -->
        <property name="expression">
            <value>execution(* *..*.getConnection())
            </value>
        </property>
        <property name="advice">
            <bean class="cn.hncu.stud.utils.ConAdvice">
            </bean>
        </property>
    </bean>

    <context:property-placeholder location="WEB-INF/conf/jdbc.properties" />
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="${driver}">
        </property>
        <property name="url" value="${url}"></property>
        <!-- ${username}是系统默认的变量-->
        <property name="username" value="${uname}"></property>
        <property name="password" value="${password}"></property>
    </bean>

</beans>

用于拦截getconnection方法的切面:

package cn.hncu.stud.utils;

import java.lang.reflect.Method;
import java.sql.Connection;

import javax.sql.DataSource;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodProxy;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class ConAdvice implements MethodInterceptor {
    private ThreadLocal<Connection> t1 = new ThreadLocal<Connection>();

    DataSource data = null;

    public DataSource getData() {
        return data;
    }

    public void setData(DataSource data) {
        this.data = data;
    }

    @Override
    public Object invoke(MethodInvocation inv) throws Throwable {
        Connection con = t1.get();
        if (con == null) {
            // 往本地线程池中放一个被屏蔽掉close()方法的con对象
            final Connection con0 = ((Connection) inv.proceed());// 当前被拦截的是getconnection返回的是一个con
            // 对con0进行代理(屏蔽close())
            Callback callback = new net.sf.cglib.proxy.MethodInterceptor() {

                @Override
                public Object intercept(Object proxiedObj, Method method,
                        Object[] args, MethodProxy proxy) throws Throwable {
                    if (method.getName().equals("close")) {
                        return null;
                    }
                    return method.invoke(con0, args);
                }
            };
            // 生成代理后的对象
            con = (Connection) Enhancer.create(Connection.class, callback);
            t1.set(con);// 把代理后的对象放进去
        }
        return con;
    }
}

beans.xml:用于配置stud模块下的类的关联

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">



    <bean id="studDao" class="cn.hncu.stud.dao.IStuJDBCDAO">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="studDao2" class="cn.hncu.stud.dao.IStuJDBCDAO2">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="service" class="cn.hncu.stud.service.serviceimpl">
        <property name="dao1" ref="studDao"></property>
        <property name="dao2" ref="studDao2"></property>
    </bean>
</beans>   


AroundAdvice:进行service类的拦截:

package cn.hncu.stud.utils;

import java.sql.Connection;

import javax.sql.DataSource;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class AroundAdvice implements MethodInterceptor, ApplicationContextAware {
    ApplicationContext ctx = null;
    DataSource data = null;

    public DataSource getData() {
        return data;
    }

    public void setData(DataSource data) {
        this.data = data;
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Connection con = data.getConnection();
        con.setAutoCommit(false);
        System.out.println("开启事务");
        // 放行
        try {
            Object returnValue = invocation.proceed();
            con.commit();
            return returnValue;
        } catch (Exception e) {
            con.rollback();
            System.out.println("事务回滚");
        } finally {
            con.setAutoCommit(true);
        }
        return null;
    }

    @Override
    public void setApplicationContext(ApplicationContext ctx)
            throws BeansException {
        this.ctx = ctx;
    }

}

这样就成功通过spring实现数据库的事务处理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaEE实验中的Spring AOP是指Spring框架中的面向切面编程。它可以在不修改原有代码的情况下,通过在程序运行时动态地将代码织入到类的指定方法中,实现对方法的增强。具体来说,Spring AOP主要包括两种代理方式:JDK动态代理和CGLIB动态代理。其中,JDK动态代理只能代理实现了接口的类,而CGLIB动态代理则可以代理任意类。在Spring AOP中,我们可以通过XML配置或注解来定义切面和通知,实现对目标方法的增强。 以下是JavaEE实验Spring AOP的基本步骤: 1. 定义切面类,即包含通知的类。 2. 定义通知,即增强的代码逻辑。 3. 配置切面和通知,可以通过XML配置或注解来实现。 4. 在需要增强的方法上添切点,即指定需要增强的方法。 5. 运行程序,观察增强效果。 下面是一个使用XML配置的Spring AOP的例子: 1. 定义切面类和通知: ```java public class LogAspect { public void before() { System.out.println("方法执行前"); } public void after() { System.out.println("方法执行后"); } } ``` 2. 在XML配置文件中定义切面和通知: ```xml <bean id="logAspect" class="com.example.LogAspect"/> <aop:config> <aop:aspect ref="logAspect"> <aop:before method="before" pointcut="execution(* com.example.Service.*(..))"/> <aop:after method="after" pointcut="execution(* com.example.Service.*(..))"/> </aop:aspect> </aop:config> ``` 3. 在需要增强的方法上添切点: ```java @Service public class UserServiceImpl implements UserService { @Override public void addUser(User user) { System.out.println("添用户:" + user.getName()); } } ``` 4. 运行程序,观察增强效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值