Animal接口必须实现接口,没有实现接口用cglib.
package com.example.test.jdk_proxy; public interface Animal { String say(); }Cat实现Animal接口
package com.example.test.jdk_proxy; public class Cat implements Animal { @Override public String say() { return "喵喵喵~"; } }测试创建动态代理对象
package com.example.test.jdk_proxy; import java.lang.reflect.Proxy; public class ProxyDemo { public static void main(String[] args) { Cat cat = new Cat(); Animal animalProxyInstance = (Animal) Proxy.newProxyInstance(Cat.class.getClassLoader(), Cat.class.getInterfaces(), (proxy, method, args1) -> { System.out.println("invoke before"); Object result = method.invoke(cat, args1); System.out.println("invoke result: " + result); System.out.println("invoke after"); return result; }); animalProxyInstance.say(); } }
运行结果:
Cat类spring对于实现了接口的类会采用jdk proxy,否则使用cglib做动态代理。
也可以配置都使用cglib
package com.example.test.cglib; public class Cat { public String write() { return "cat write letter"; } }创建代理对象
package com.example.test.cglib; import org.springframework.cglib.proxy.Enhancer; import org.springframework.cglib.proxy.MethodInterceptor; public class CgLibDemo { public static void main(String[] args) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(Cat.class); enhancer.setCallback((MethodInterceptor) (object, method, args1, methodProxy) -> { System.out.println("invoke before"); Object result = methodProxy.invokeSuper(object, args1); System.out.println("invoke result: " + result); System.out.println("invoke after"); return result; }); Cat cat = (Cat) enhancer.create(); cat.write(); } }
运行结果:
package com.example.test.aspectj; import org.springframework.stereotype.Component; @Component public class Dog { public String say() { return "汪汪汪"; } }切面类
package com.example.test.aspectj.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; @Aspect @Component public class DogAspect { @Pointcut("execution(String com.example.test.aspectj.Dog.say())") public void dogSay() { } @Before(value = "dogSay()") public void before() { System.out.println("@Before executed"); } @After(value = "dogSay()") public void after() { System.out.println("@After executed"); } @Around(value = "dogSay()") public Object around(ProceedingJoinPoint joinPoint) { System.out.println("@Around before executed"); Object result = null; try { result = joinPoint.proceed(); System.out.println("@Around proceed result: " + result); } catch (Throwable e) { e.printStackTrace(); } System.out.println("@Around after executed"); return result; } @AfterReturning(value = "dogSay()", returning = "returnObject") public void afterReturning(JoinPoint joinPoint, Object returnObject) { System.out.println("@AfterReturning,result: " + returnObject); } @AfterThrowing(value = "dogSay()", throwing = "e") public void afterThrowing(Throwable e) { System.err.println("@AfterThrowing,error message: " + e.getMessage()); } }启动类
package com.example.test.aspectj; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationListener; @SpringBootApplication public class Application implements ApplicationListener { @Autowired private Dog dog; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) { System.out.println("call result: " + dog.say()); } }
运行结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)