Spring AOP 注解的使用

本贴最后更新于 3080 天前,其中的信息可能已经时移俗易

1、AOP 用在哪些方面

  • AOP 能够将那些与业务无关,却为业务模块所共同调用的逻辑或责任,例如事务处理、日志管理、权限控制,异常处理等,封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可操作性和可维护性。
  • 使用切面编程(AOP)需要的 jar: aspectjrt.jar、aspectjweaver.jar、cglib-nodep-2.1_3.jar

2. AOP 中的概念:

* Aspect(切面)**:指横切性关注点的抽象即为切面,它与类相似,只是两者的关注点不一样,类是对物体特征的抽象,而切面是横切性关注点的抽象. * joinpoint(连接点)**:所谓连接点是指那些被拦截到的点(**可以是方法、属性、或者类的初始化时机**(**可以是Action层、Service层、dao层)**)。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点,实际上joinpoint还可以是field或类构造器) * Pointcut(切入点)**:所谓切入点是指我们要对那些joinpoint进行拦截的定义,也即**joinpoint的集合**. * Advice(通知):**所谓通知是指拦截到joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知 * Target(目标对象)**:代理的目标对象 * Weave(织入)**:指将aspects应用到target对象并导致proxy对象创建的过程称为织入. * Introduction(引入):**在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.

Spring 的配置文件中配置 Bean,和 AOP 命名空间

<?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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="com.itsoft"/> <aop:aspectj-autoproxy /> </beans>

Example 1

@Aspect @Component public class ExampleAspect { //切入点要拦截的类 @Pointcut("execution (* *(..))") private void anyMethod(){} //声明一个切入点,切入点的名称其实是一个方法 //前置通知(不需要获取输入参数) @Before("anyMethod()")//第一个参数为切入点的名称 public void doAccessCheck(){ System.out.println("前置通知"); } //后置通知(不需要获取返回值) @AfterReturning("anyMethod()") public void doAfterReturning(){ System.out.println("后置通知:"); } //例外通知(不需要异常信息) @AfterThrowing("anyMethod()") public void doAfterThrowing(){ System.out.println("例外通知"); } //最终通知 @After("anyMethod()") public void doAfter(){ System.out.println("最终通知"); } //环绕通知(适合做权限系统) @Around("anyMethod()") public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("环绕通知进入方法"); Object object=pjp.proceed(); System.out.println("环绕通知退出方法"); return object; } }

Example 2

@Aspect @Component public class Example2Aspect { //切入点要拦截的类 @Pointcut("execution (* *(..))") private void anyMethod(){} //声明一个切入点,切入点的名称其实是一个方法 //前置通知(获取输入参数) @Before("anyMethod() && args(name)")//第一个参数为切入点的名称,第二个是测试获取输入参数,此处为string类型的,参数名称与方法中的名称相同,如果不获取输入参数,可以不要 public void doAccessCheck(String name){ System.out.println("前置通知:"+name); } //后置通知(获取返回值) @AfterReturning(pointcut="anyMethod()", returning="result") public void doAfterReturning(String result){ System.out.println("后置通知:"+result); } //例外通知(获取异常信息) @AfterThrowing(pointcut="anyMethod()",throwing="e") public void doAfterThrowing(Exception e){ System.out.println("例外通知:"+e); } //最终通知 @After("anyMethod()") public void doAfter(){ System.out.println("最终通知"); } //环绕通知(特别适合做权限系统) @Around("anyMethod()") public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("环绕通知进入方法"); Object object=pjp.proceed(); System.out.println("环绕通知退出方法"); return object; } }
  • AOP
    21 引用 • 13 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...