首页 > 技术文章 > 基于JDK,AspectJ实现的动态代理demo

yxby 2022-03-04 20:50 原文

1 package com.pojo;
2 
3 public interface Animal {
4     String eat(String foodName);
5     int sleep(int time);
6 }
Animal接口
 1 package com.pojo.impl;
 2 
 3 import com.pojo.Animal;
 4 
 5 public class Pig implements Animal {
 6 
 7     @Override
 8     public String eat(String foodName) {
 9         System.out.println("吃了"+foodName);
10         return foodName;
11     }
12 
13     @Override
14     public int sleep(int time) {
15         System.out.println("睡了"+time);
16         return time;
17     }
18 }
实现Animal接口的Pig类
package com.pojo.impl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;


public class ProxyPig implements InvocationHandler {

    private Object obj;
    public ProxyPig(Object obj){
        this.obj = obj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //方法之前做一个处理
        System.out.println("方法之前执行......\t"+method.getName()+"\t"+Arrays.toString(args));

        //被增强的方法
        Object value = method.invoke(obj,args);

        //方法之后执行
        System.out.println("方法之后执行\t"+value);

        return value;
    }
}
实现InvocationHandler接口的类
package testMain;

import com.pojo.Animal;
import com.pojo.impl.Pig;
import com.pojo.impl.ProxyPig;
import java.lang.reflect.Proxy;

public class Main {
    public static void main(String[] args) {
        Class[] c = {Animal.class};

        Animal pigAnimal = (Animal) Proxy.newProxyInstance(Pig.class.getClassLoader(), c, new ProxyPig(new Pig()));

        pigAnimal.eat("包子");
        pigAnimal.sleep(1000);
    }
}
主方法内进行测试

 以上是基于JDK实现的动态代理demo

1 @Service()
2 public class Login {
3     public Boolean login(){
4         System.out.println("被增强方法");
5         //int a = 1/0;
6         return true;
7     }
8 }
Login登录类
 1 @Service
 2 @Aspect
 3 public class LoginProxy {
 4 
 5     @Pointcut(value = "execution(* com.service.Login.login())")
 6     public void pointDemo(){
 7 
 8     }
 9 
10     //Before作为前置通知
11     @Before(value = "pointDemo()")
12     public void before(){
13         System.out.println("前置");
14     }
15 
16     @After(value = "pointDemo()")
17     public void after(){
18         System.out.println("最终!");
19     }
20 
21     @AfterReturning(value = "pointDemo()")
22     public void AfterReturning(){
23         System.out.println("后置");
24     }
25 
26     @AfterThrowing(value = "pointDemo()")
27     public void afterThrowing(){
28         System.out.println("异常");
29     }
30 
31     @Around(value = "pointDemo()")
32     public void afterReturning(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
33         System.out.println("环绕前");
34 
35         //被增强的方法执行
36         proceedingJoinPoint.proceed();
37 
38         System.out.println("环绕后");
39     }
40 }
增强类
@Configuration
@ComponentScan(basePackages = {"com"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class AnnotaionCon {
}
配置类
public class Main {
    public static void main(String[] args){
        ApplicationContext context = new AnnotationConfigApplicationContext(AnnotaionCon.class);
        Login login = context.getBean("login", Login.class);
        login.login();
    }
}
主方法内进行测试
 1 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
 2     <dependency>
 3       <groupId>org.springframework</groupId>
 4       <artifactId>spring-core</artifactId>
 5       <version>5.2.19.RELEASE</version>
 6     </dependency>
 7     <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
 8     <dependency>
 9       <groupId>org.springframework</groupId>
10       <artifactId>spring-beans</artifactId>
11       <version>5.2.19.RELEASE</version>
12     </dependency>
13     <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
14     <dependency>
15       <groupId>org.springframework</groupId>
16       <artifactId>spring-context</artifactId>
17       <version>5.2.19.RELEASE</version>
18     </dependency>
19     <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
20     <dependency>
21       <groupId>org.springframework</groupId>
22       <artifactId>spring-expression</artifactId>
23       <version>5.2.19.RELEASE</version>
24     </dependency>
25     <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
26     <dependency>
27       <groupId>commons-logging</groupId>
28       <artifactId>commons-logging</artifactId>
29       <version>1.1.1</version>
30     </dependency>
31     <!--Spring基本模块-->
32 
33     <!-- https://mvnrepository.com/artifact/net.sourceforge.cglib/com.springsource.net.sf.cglib -->
34     <dependency>
35       <groupId>net.sourceforge.cglib</groupId>
36       <artifactId>com.springsource.net.sf.cglib</artifactId>
37       <version>2.2.0</version>
38     </dependency>
39     <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
40     <dependency>
41       <groupId>org.aspectj</groupId>
42       <artifactId>aspectjweaver</artifactId>
43       <version>1.9.6</version>
44     </dependency>
45     <!--aspectj基本模块-->
需要导入的jar包

以上是基于AspectJ实现的动态代理demo

 

推荐阅读