spring AOP
软件151 秦康维
spring AOP
aop代码实例:
1.创建一个控制台程序;导入包
2.创建接口
[java] view plain copy
package com.AspectTest.service;
public interface personServer {
public void save(String uname,int age);
}
3.接口的实现
[java] view plain copy
package com.AspectTest.service;
public class personServerImpl implements personServer{
@Override
public void save(String uname,int age) {
int a=0;
age= age/a;//打开上面两行报错,可触发异常通知
System.out.println("come in personServerImpl save method...");
}
}
4.创建(aop)切面类
[java] view plain copy
package com.AspectTest.aop;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect//声明这是一个切面
@Component//声明这是一个组件,泛指...可以去掉
public class AspectIntercepter {
@Pointcut(value="execution(* com.AspectTest.service.personServerImpl.*(..))")
private void pointCut(){//定义一个切入点 后面的通知直接引入切入点方法pointCut即可 personServerImpl下面的所有方法
}
//环绕通知(连接到切入点开始执行,下一步进入前置通知,在下一步才是执行操作方法)
@Around(value="pointCut()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("@Around进入环绕通知...");
Object object = pjp.proceed();//执行该方法
System.out.println(pjp.getThis()+" 操作结束,退出方法;环绕[@Around]结束!...");
return object;
}
//前置通知(进入环绕后执行,下一步执行方法)
@Before(value="pointCut()")
public void doAccessCheck(JoinPoint joinPoint){
System.out.println("@Before前置通知:"+Arrays.toString(joinPoint.getArgs()));
}
//异常通知(出错时执行)
@AfterThrowing(value="pointCut()",throwing="ex")
public void doAfterThrow(JoinPoint joinPoint,Throwable ex){
System.out.println("@AfterThrowing例外通知(异常通知)"+Arrays.toString(joinPoint.getArgs()));
System.out.println("@AfterThrowing异常信息:"+ex);
}
//后置通知(返回之前执行)
@After(value="pointCut()")
public void after(){
System.out.println("@After后置通知...");
}
//最终通知(正常返回通知,最后执行)
@AfterReturning(value="pointCut()")
public void doAfter(){
System.out.println("@AfterReturning最终通知...End!");
}
}
5.配置aop配置文件
[html] view plain copy
<?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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 使 AspectJ 的注解起作用 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="personServiceBean" class="com.AspectTest.service.personServerImpl"/>
<bean id="myInterceptor" class="com.AspectTest.aop.AspectIntercepter"/>
</beans>
6.创建测试类
[java] view plain copy
package com.AspectTest;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.AspectTest.service.personServer;
public class SpringAopTest {
@Test
public void inteceptorTest(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-aop.xml");
personServer bean = (personServer)ctx.getBean("personServiceBean");
bean.save("badMonkey",23);
// personServer p = new personServerImpl();//通过new对象是不会触发aop的
// p.save("11", "22");
}
}
【运行结果1】:
@Around进入环绕通知...
@Before前置通知:[badMonkey, 23]
come in personServerImpl save method...
com.AspectTest.service.personServerImpl@11023cad 操作结束,退出方法;环绕
[@Around]结束!...
@After后置通知...
@AfterReturning最终通知...End!
【运行结果2】:
@Around进入环绕通知...
@Before前置通知:[badMonkey, 23]
@After后置通知...
@AfterThrowing例外通知(异常通知)[badMonkey, 23]
@AfterThrowing异常信息:Java.lang.ArithmeticException: / by zero