注解实现的spring-aop
2021-06-29 05:04
标签:work new 切面 img targe result join alt void 接口: 实现类: 配置文件:aop.xml 验证切面: 主切面 测试类 结果
注解实现的spring-aop 标签:work new 切面 img targe result join alt void 原文地址:https://www.cnblogs.com/kill-9/p/9647347.html 1 package spring.aop;
2
3 public interface Arithmetic {
4
5 Integer add(Integer a,Integer b);
6 Integer sub(Integer a,Integer b);
7 Integer div(Integer a,Integer b);
8
9
10 }
1 package spring.aop;
2
3 import org.springframework.stereotype.Component;
4
5 @Component
6 public class ArithmeticImpl implements Arithmetic{
7
8 @Override
9 public Integer add(Integer a, Integer b) {
10 System.out.println("add -> "+(a+b));
11 return a+b;
12 }
13
14 @Override
15 public Integer sub(Integer a, Integer b) {
16 System.out.println("sub -> "+(a-b));
17 return a-b;
18 }
19
20 @Override
21 public Integer div(Integer a, Integer b) {
22 System.out.println("div -> "+(a/b));
23 return a/b;
24 }
25
26
27 }
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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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="spring.aop"/>
aop:aspectj-autoproxy proxy-target-class="true" />
beans>
1 package spring.aop;
2
3 import org.aspectj.lang.JoinPoint;
4 import org.aspectj.lang.annotation.Aspect;
5 import org.aspectj.lang.annotation.Before;
6 import org.springframework.core.annotation.Order;
7 import org.springframework.stereotype.Component;
8
9 import java.util.Arrays;
10
11 @Order(1)
12 @Aspect
13 @Component
14 public class VliAspect {
15
16 @Before("spring.aop.aspect.declareJointPointExpression()")
17 public void validateArgs(JoinPoint joinPoint){
18
19 System.out.println("validateArgs -> "+ Arrays.asList(joinPoint.getArgs()));
20 }
21 }
1 package spring.aop;
2
3 import org.aspectj.lang.JoinPoint;
4 import org.aspectj.lang.annotation.*;
5 import org.springframework.core.annotation.Order;
6 import org.springframework.stereotype.Component;
7
8 import java.util.Arrays;
9 import java.util.List;
10
11 //声明这个切面的优先级,值越小,优先级越高
12 @Order(2)
13 //声明这个类是一个切面
14 @Aspect
15 @Component
16 public class aspect {
17
18 //定义一个方法,用于声明切点表达式,一般该方法不写其他代码
19 @Pointcut("execution( * spring.aop.*.*(*,*))")
20 public void declareJointPointExpression(){}
21
22 //声明该方法是一个前置通知
23 @Before("declareJointPointExpression()")
24 public void beforeMethod(JoinPoint joinPoint){
25 String methodName = joinPoint.getSignature().getName();
26 List
package spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args){
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:aop.xml");
Arithmetic proxy = ac.getBean("arithmeticImpl",ArithmeticImpl.class);
proxy.add(3,5);
System.out.println("-----------------------");
proxy.sub(3,0);
System.out.println("-----------------------");
proxy.div(3,0);
System.out.println("-----------------------");
}
}