1. 什么是aop?
aop就是面向切面编程,可以横向的去控制一群类的某些方法,在这些方法上加上前置和后置等操作,aop编程很方便我们实现对事务、日志、异常等的统一管理。
2. aop的通知类型共五种
before:前置通知(应用:各种校验)
在方法执行之前执行,如果其中抛出异常,无法执行
after: 后置通知(应用:清理现场)
方法执行完毕后执行,无论方法中是否出现异常
afterReturning: 返回后执行通知(应用:常规数据处理)
方法正常返回后执行,如果方法中抛出异常,无法执行
afterThrowing: 抛出异常后通知(应用:包装异常信息)
方法抛出异常后执行,如果方法没有抛出异常无法执行
around: 环绕通知(应用:十分强大,可以做任何事情)
方法执行前后分别执行,可以阻止方法的执行
3. 事务管理实例(注解)
1.导入jar包
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons.logging-1.1.1.jar
log4j-1.2.15.jar
spring-aop-3.2.0.RELEASE.jar
spring-aspects-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-test-3.2.0.RELEASE.jar
2.xml配置
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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" >
<!-- 设置扫描路径 -->
<context:component-scan base-package="com.lyn.*"></context:component-scan>
<!-- 开启aop自动代理 -->
<aop:aspectj-autoproxy/>
</beans>
3.注解配置
package com.lyn.advice;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component("ta")
@Aspect //告诉spring这个类是通知类 <aop:aspect ref="ta"
public class TransactionAdvice {
//必须是常量
public static final String EX = "execution(* com.lyn.service.*.*(..))";
//开启事务 通知(被抽取出来组成独立代 码逻辑的共性功能称为通知)
@Before(EX)//<aop:before method poingcut="">
public void open(JoinPoint jp){ // JoinPoint Joinpoint
Object[] args = jp.getArgs();
System.out.println(Arrays.toString(args));
System.out.println("开启事务");
}
//提交事务 通知
@AfterReturning(EX)
public void commit(){
System.out.println("提交事务");
}
//回滚事务 通知
@AfterThrowing(value="execution(* com.lyn.service.*.*(..))")
public void rollBack(){
System.out.println("回滚事务");
}
//环绕通知
public void around(ProceedingJoinPoint pjp){
System.out.println("开启事务");
try {
//调用目标对象的方法
Object obj = pjp.proceed(); //obj为目标对象方法的返回值
System.out.println("值:"+obj);
} catch (Throwable e) {
e.printStackTrace();
System.out.println("回滚事务");
}
System.out.println("提交事务");
}
}
4.测试
package com.lyn.action;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import com.lyn.service.UserService;
import com.lyn.service.UserServiceImpl;
@Controller("userAction")
public class UserAction {
//private UserService us = new UserServiceImpl();
@Resource(name="userService")
private UserService us ; //该资源使用spring来注入 (代理对象)
public void addUser(){
us.add();
}
public void deleteUser(){
us.delete(4,5);
}
}
package com.lyn.service;
import org.springframework.stereotype.Service;
import com.lyn.advice.TransactionAdvice;
@Service("userService")
public class UserServiceImpl implements UserService {
//连接点(类中的方法 ) 切入点(被抽取了共性特征的方法叫 切入点)
public void add() {
System.out.println("UserServiceImpl.add()");
//int a = 1/0;
}
//连接点 切入点
public int delete(int a ,int b) {
System.out.println("UserServiceImpl.delete()");
return 5;
}
//连接点
public void find(){
}
}
package com.lyn.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.lyn.action.UserAction;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:/applicationContext.xml")
public class UserTestAno {
@Resource(name="userAction")
private UserAction ua;
@Test
public void fn1(){
ua.addUser();
}
@Test
public void fn2(){
ua.deleteUser();
}
}
5.出现问题
java.lang.IllegalStateException: Failed to load ApplicationContext
解决方案:jdk1.8 –> jdk1.7
jdk1.8遇到好多类似这样的问题,改了代码好久就是不知道哪里问题,真是不好用。