基于JDK,AspectJ实现的动态代理demo


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 
 2     <dependency>
 3       <groupId>org.springframeworkgroupId>
 4       <artifactId>spring-coreartifactId>
 5       <version>5.2.19.RELEASEversion>
 6     dependency>
 7     
 8     <dependency>
 9       <groupId>org.springframeworkgroupId>
10       <artifactId>spring-beansartifactId>
11       <version>5.2.19.RELEASEversion>
12     dependency>
13     
14     <dependency>
15       <groupId>org.springframeworkgroupId>
16       <artifactId>spring-contextartifactId>
17       <version>5.2.19.RELEASEversion>
18     dependency>
19     
20     <dependency>
21       <groupId>org.springframeworkgroupId>
22       <artifactId>spring-expressionartifactId>
23       <version>5.2.19.RELEASEversion>
24     dependency>
25     
26     <dependency>
27       <groupId>commons-logginggroupId>
28       <artifactId>commons-loggingartifactId>
29       <version>1.1.1version>
30     dependency>
31     
32 
33     
34     <dependency>
35       <groupId>net.sourceforge.cglibgroupId>
36       <artifactId>com.springsource.net.sf.cglibartifactId>
37       <version>2.2.0version>
38     dependency>
39     
40     <dependency>
41       <groupId>org.aspectjgroupId>
42       <artifactId>aspectjweaverartifactId>
43       <version>1.9.6version>
44     dependency>
45     
需要导入的jar包

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