Spring3——使用注解实现声明式事务、面向切面编程——AOP

使用注解实现声明式事务
1.jar包
spring-tx-4.3.9.RELEASE
mysql-connector-java-5.1.47.jar
common-dbcp.jar 连接池使用数据源
common-pool.jar 连接池
spring-jdbc-4.3.9.RELEASE
aopalliance.jar
2.配置
增加事务tx的命名空间。
<!--配置数据库相关-事务-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="mysql://127.0.0.1:3306/springDB"></property>
    <property name="username" value="root"></property>
    <property name="password" value="123456"></property>
    <property name="maxActive" value="10"></property>
    <property name="maxIdle" value="6"></property>
</bean>

<!--配置事务管理器txManager-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
</bean>

<!--增加对事务的支持-->
<tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>
3.使用
将需要成为事务的方法前增加注解。
@Transactional(readOnly = false,propagation = Propagation.REQUIRED)

面向切面编程——AOP  

普通类——>特定功能的类:
(1)继承类(2)实现接口(3)注解(4)配置
 
通过接口实现aop
类——>“通知”:
实现接口。

 

前置通知实现步骤:
(1)jar:
aopalliance.jar
aspectjweaver.jar
(2)配置   
<!--addStudent()所在的方法-->
<bean id="studentService" class="org.ghl.service.StudentServiceImpl">
    <property name="studentDao" ref="studentDao"></property>
</bean>


<!--“前置通知”类-->
<!--======连接线的一方======-->
<bean id="logBefore" class="org.ghl.aop.LogBefore">
</bean>


<!--将addStudent()与通知相连-->
<aop:config>
    <!--配置切入点(在哪里执行通知)-->
    <!--=====连接线的另一方======-->
    <aop:pointcut expression="execution(public void org.ghl.service.StudentServiceImpl.addStudent(org.ghl.entity.Student)) or execution
    (public void org.ghl.service.StudentServiceImpl.deleteStudentByNo(int))" id="pointcut"></aop:pointcut> <!--advisor相当于连接切入点和切面的线--> <!--=======连接线=======--> <aop:advisor advice-ref="logBefore" pointcut-ref="pointcut"/> </aop:config>

(3)编写 

实现接口:
public class LogBefore implements MethodBeforeAdvice

表达式expression常见示例

后置通知实现步骤:
(1)通知类,实现接口
(2)业务类,业务方法
(3)将业务类、通知纳入springIOC容器;
定义切入点、定义通知类,通过pointcut-ref将两端连接起来。
 
异常通知实现步骤:
异常通知的实现类必须实现以下方法。
public void afterThrowing(Method method,Object[] args,Object target, Throwable ex){}
 
环绕通知实现步骤:
可以获得目标方法的全部控制权。
(1)通public class LogAround implements MethodInterceptor {     @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Object result = null;
        //方法体1
        try {
            //方法体2
            System.out.println("用环绕实现【前置通知】...");
            //invocation.proceed();前为前置通知
            result=invocation.proceed();//控制目标方法的执行
            //result就是目标方法addStudent()的返回值
            //invocation.proceed();后为后置通知
            System.out.println("用环绕实现【后置通知】...");
            System.out.println("用环绕实现后置通知:目标对象:"+invocation.getThis()+",调用的方法名:"+invocation.getMethod().getName()+",方法的参数个数:"+
invocation.getArguments().length+",方法的返回值:"+result); } catch (Exception ex) { //方法体3 // 异常通知 System.out.println("用环绕实现【异常通知】..."); } return result; } }

(2)业务类

(3)配置
使用环绕通知时,目标方法的一切信息可以通过innovation参数获取到。
环绕通知底层通过拦截器实现。
 
 

猜你喜欢

转载自www.cnblogs.com/ghlz/p/13180728.html