SpringAOP前置和后置通知

AOP 配置:
加入 JAR 包
com.springsource.net.sf.cglib-2.2.0.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
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
在配置文件中加入 AOP 的命名空间在配置文件中加入
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
把横切关注点的代码抽象到切面类中.切面首先是一个 IOC 中的 bean, 即加入 @Component 注解切面还需加入 @Aspect 注解在类中声明各种通知:声明一个方法在方法前加入 @Before 注解可以在通知方法中声明一个类型 JoinPoint 的参数, 然后就能访问链接细节. 如方法名称和参数
[Java]  纯文本查看  复制代码
?
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
public interface ArithmeticCalculator {
         int add( int i, int j);
         int sub( int i, int j);
         int mul( int i, int j);
         int div( int i, int j);
}
 
@Component ( "arithmeticCalculator" )
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
         @Override
         public int add( int i, int j) {
                 int result = i + j;
                 return result;
         }
         @Override
         public int sub( int i, int j) {
                 int result = i - j;
                 return result;
         }
         @Override
         public int mul( int i, int j) {
                 int result = i * j;
                 return result;
         }
         @Override
         public int div( int i, int j) {
                 int result = i / j;
                 return result;
         }
}
 
/**
  * AOP 的 helloWorld
  * 1. 加入 jar 包
  * com.springsource.net.sf.cglib-2.2.0.jar
  * com.springsource.org.aopalliance-1.0.0.jar
  * com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
  * spring-aspects-4.0.0.RELEASE.jar
  *
  * 2. 在 Spring 的配置文件中加入 aop 的命名空间。
  *
  * 3. 基于注解的方式来使用 AOP
  * 3.1 在配置文件中配置自动扫描的包: <context:component-scan base-package="com.spring.aop.Impl"></context:component-scan>
  * 3.2 加入使 AspjectJ 注解起作用的配置: <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  * 为匹配的类自动生成动态代理对象.
  *
  * 4. 编写切面类:
  * 4.1 一个一般的 Java 类
  * 4.2 在其中添加要额外实现的功能.
  *
  * 5. 配置切面
  * 5.1 切面必须是 IOC 中的 bean: 实际添加了 @Component 注解
  * 5.2 声明是一个切面: 添加 @Aspect
  * 5.3 声明通知: 即额外加入功能对应的方法.
  * 5.3.1 前置通知: @Before("execution(public int com.spring.aop.ArithmeticCalculator.*(int, int))")
  * @Before 表示在目标方法执行之前执行 @Before 标记的方法的方法体.
  * @Before 里面的是切入点表达式:
  *
  * 6. 在通知中访问连接细节: 可以在通知方法中添加 JoinPoint 类型的参数, 从中可以访问到方法的签名和方法的参数.
  *
  * 7. @After 表示后置通知: 在方法执行之后执行的代码.
  */
//通过添加 @Aspect 注解声明一个 bean 是一个切面!
@Aspect
@Component
public class LoggingAspect {
         @Before ( "execution(public int com.spring.aop.Impl.ArithmeticCalculator.add(int, int))" )
         public void beforeMethod(JoinPoint joinPoint){
                 String methodName = joinPoint.getSignature().getName();
                 Object [] args = joinPoint.getArgs();
                 
                 System.out.println( "The method " + methodName + " begins with " + Arrays.asList(args));
         }
         @After ( "execution(* com.spring.aop.Impl.*.*(int, int))" )
         public void afterMethod(JoinPoint joinPoint){
                 String methodName = joinPoint.getSignature().getName();
                 System.out.println( "The method " + methodName + " ends" );
         }
}
 
<?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 [url=http://www.springframework.org/schema/beans/spring-beans.xsd]http://www.springframework.org/schema/beans/spring-beans.xsd[/url]
                 [url=http: //www.springframework.org/schema/aop]http://www.springframework.org/schema/aop[/url] [url=http://www.springframework.org/schema/aop/spring-aop-4.0.xsd]http://www.springframework.org/schema/aop/spring-aop-4.0.xsd[/url]
                 [url=http: //www.springframework.org/schema/context]http://www.springframework.org/schema/context[/url] [url=http://www.springframework.org/schema/context/spring-context-4.0.xsd]http://www.springframework.org/s ... ing-context-4.0.xsd[/url]">
         <context:component-scan base- package = "com.spring.aop.Impl" ></context:component-scan>
         <!-- 使 AspjectJ 注解起作用: 自动为匹配的类生成代理对象 -->
         <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
 
 
public class Main {
         
         public static void main(String[] args) {
                  
                 //1. 创建 Spring 的 IOC 容器
                 ApplicationContext ctx = new ClassPathXmlApplicationContext( "spring-aop.xml" );
                 
                 //2. 从 IOC 容器中获取 bean 实例
                 ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator. class );
                 
                 //3. 使用 bean
                 int add = arithmeticCalculator.add( 2 , 2 );
                 System.out.println(add);
         }
         
}
更多学习资料可关注:itheimaGZ获取
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值