java aop 注解方式_Spring AOP的注解方式实现

spring也支持注解方式实现AOP,相对于配置文件方式,注解配置更加的轻量级,配置、修改更加方便。

1.开启AOP的注解配置方式

2.将定制的类标志为一个切面

9877c2a45051a78e777e4941443d2f6f.png

3.配置通知,指定切入点规则

前置通知 @Before

环绕通知 @Around

后置通知 @AfterReturning

异常通知 @AfterThrowing

最终通知 @After

@Before("execution(* cn.tedu.service.*.*(..))")public voidbefore(JoinPoint jp){

Class clz=jp.getTarget().getClass();

Signature signature=jp.getSignature();

String name=signature.getName();

System.out.println("before...["+clz+"]...["+name+"]...");

}

** 通过注解的配置 等价于 配置文件的配置

6aced1bd97bc1810c7672f4aaf34c982.png

4.重复使用同一个切入点表达式

如果一个切面中多个通知 重复使用同一个切入点表达式,则可以将该切入点表达式单独定义,后续引用。

注意,在当前切面中通过注解定义的切入点只在当前切面中起作用,其他切面看不到。

ec1ad982554a3373cafdc0d8c9dac9c8.png

5.额外配置一个returning属性

在后置通知的注解中,也可以额外配置一个returning属性,来指定一个参数名接受目标方法执行后的返回值。

0a6553cd1fe7f382a98aca1aa84ce53f.png

6.异常通知的注解

在异常通知的注解中,也可以额外配置一个throwing属性,来指定一个参数名接受目标方法抛出的异常对象。

debd5bf9c6ab229479f7c0ec70ca0a3b.png

源码

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd "

>

packagecn.tedu.service;public interfaceUserService {publicString addUser(String name);public voidupdateUser();public voiddeleteUser();public voidquery();

}

packagecn.tedu.service;importorg.springframework.stereotype.Service;

@Service("userService")public class UserServiceImple implementsUserService {

@OverridepublicString addUser(String name) {//int i = 10/0;

System.out.println("增加用户。。");return "cjj";

}

@Overridepublic voidupdateUser() {

System.out.println("修改用户。。");

}

@Overridepublic voiddeleteUser() {

System.out.println("删除用户。。");

}

@Overridepublic voidquery() {

System.out.println("查询用户。。");

}

}

packagecn.tedu.aop;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.Signature;importorg.aspectj.lang.annotation.After;importorg.aspectj.lang.annotation.AfterReturning;importorg.aspectj.lang.annotation.AfterThrowing;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Before;importorg.aspectj.lang.annotation.Pointcut;importorg.springframework.stereotype.Component;

@Component

@Aspectpublic classFirstAspect {

@Pointcut("execution(* cn.tedu.service.*.*(..))")public voidms(){

}

@Before("ms()")public voidbefore(JoinPoint jp){

Class clz=jp.getTarget().getClass();

Signature signature=jp.getSignature();

String name=signature.getName();

System.out.println("before...["+clz+"]...["+name+"]...");

}

@Around("ms()")public Object around(ProceedingJoinPoint jp) throwsThrowable{

System.out.println("1around before...");

Object obj= jp.proceed();//--显式的调用目标方法

System.out.println("1around after...");returnobj;

}

@AfterReturning(value="ms()",returning="msg")public voidafterReturn(String msg){

System.out.println("1 -- afterReturn..." +msg);

}

@AfterThrowing(value="ms()",throwing="e")public voidaftrThrow(Throwable e){

System.out.println("1 -- afterThrow..." +e.getMessage());

}

@After("ms()")public voidafter(){

System.out.println("1 -- after..");

}

}

packagecn.tedu.test;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcn.tedu.service.UserService;public classAOPTest {

@Testpublic voidtest01(){

ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");

UserService userService= (UserService) context.getBean("userService");

userService.addUser("cjj"); //一个连接点

}

}


版权声明:本文为weixin_31758889原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。