博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring AOP 原理
阅读量:5082 次
发布时间:2019-06-13

本文共 5420 字,大约阅读时间需要 18 分钟。

静态代理

public class ProxyDemo {    public static void main(String[] args) {        RealImage realImage = new RealImage();        ProxyImage proxyImage = new ProxyImage(realImage);        proxyImage.doSomething();    }}interface Image {    void doSomething();}class RealImage implements Image {    @Override    public void doSomething() {        System.out.println("Real image");    }}class ProxyImage implements Image {    private RealImage realImage;    public ProxyImage(RealImage realImage) {        this.realImage = realImage;    }    @Override    public void doSomething() {        System.out.println("proxy start");        realImage.doSomething();        System.out.println("proxy end");    }}

 

打印结果:

proxy startReal imageproxy end

 

JDK 动态代理(基于接口)

import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;public class ProxyDemo {    public static void main(String[] args) {        Target target = new TargetImpl();        TargetIH targetIH = new TargetIH(target);        Class
clazz = target.getClass(); Target proxyInstance = (Target) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), targetIH); proxyInstance.doSomething(); proxyInstance.doSomethingElse(); }}interface Target { void doSomething(); void doSomethingElse();}class TargetImpl implements Target { @Override public void doSomething() { System.out.println("TargetImpl doSomething"); } @Override public void doSomethingElse() { System.out.println("TargetImpl doSomethingElse"); }}class TargetIH implements InvocationHandler { private Object target; public TargetIH(Object target) { this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) { System.out.println("proxy start"); Object result = null; try { result = method.invoke(target, args); } catch (Exception e) { // do something } System.out.println("proxy end"); return result; }}

 

打印:

proxy startTargetImpl doSomethingproxy endproxy startTargetImpl doSomethingElseproxy end

 

CGLIB

 (Byte Code Generation Library is high level API to generate and transform Java byte code. It is used by AOP, testing, data access frameworks to generate dynamic proxy objects and intercept field access)动态代理,不需要有接口

cglib
cglib
3.2.5

 

import java.lang.reflect.Method;import net.sf.cglib.proxy.Enhancer;import net.sf.cglib.proxy.MethodInterceptor;import net.sf.cglib.proxy.MethodProxy;public class ProxyDemo {    public static void main(String[] args) {                TargetMI interceptor = new TargetMI();        Enhancer e = new Enhancer();        e.setSuperclass(Target.class);        e.setCallback(interceptor);        Target proxyInstance = (Target) e.create();        proxyInstance.doSomething();    }}class Target {    public void doSomething() {        System.out.println("doSomething");    }}class TargetMI implements MethodInterceptor {    @Override    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {        System.out.println("proxy start");        Object retValFromSuper = proxy.invokeSuper(obj, args);        System.out.println("proxy end");        return retValFromSuper;    }}

打印:

proxy startdoSomethingproxy end

 

Spring AOP

org.springframework
spring-context
4.3.3.RELEASE
org.aspectj
aspectjweaver
1.8.10
package cn.zno.foo;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.stereotype.Component;public class ProxyDemo {    public static void main(String[] args) {        @SuppressWarnings("resource")        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");        Target target = (Target) ctx.getBean(Target.class);        target.doSomething();        target.doSomethingElse();    }}interface Target {    public void doSomething();    public void doSomethingElse();}@Componentclass TargetImpl implements Target {    @Override    public void doSomething() {        System.out.println("doSomething");    }    @Override    public void doSomethingElse() {        System.out.println("doSomethingElse");    }}@Component // 需要注册到bean工厂,否则不会生效@Aspect // 需要配置Spring AOPclass TargetInterceptor {        @Pointcut("execution(* doSomething(..)) || execution(* doSomethingElse(..))") // the pointcut(target method) signature    private void anysdf() {        System.out.println("pointcut signature");    }    @Before(value = "anysdf()")    public void before(JoinPoint joinPoint) {        System.out.println("Before");    }    @After("anysdf()")    public void after() {        System.out.println("After");    }}

打印:

BeforedoSomethingAfterBeforedoSomethingElseAfter

 

 

 

转载于:https://www.cnblogs.com/zno2/p/7522134.html

你可能感兴趣的文章
MapReduce的运行原理
查看>>
Leetcode: Partition List
查看>>
故障转移
查看>>
清空dataset中的某行某列的数据
查看>>
盒模型
查看>>
js中闭包和作用域
查看>>
关键词提取
查看>>
装饰器,迭代器与生成器
查看>>
endpoint 理解-1
查看>>
Lambad表达式树(转)
查看>>
<每日一题>题目28:简单的python练习题(51-60)
查看>>
15 SharePreference
查看>>
24点
查看>>
各种米的营养价值
查看>>
php 处理数字为金钱格式
查看>>
学习51单片机——秒表分享
查看>>
我画你猜(微信版--游戏说明)
查看>>
cssText的用法以及特点 转载至http://www.cnblogs.com/majingyi/p/6840818.html
查看>>
7款纯CSS3实现的炫酷动画应用
查看>>
sed结构分析 + awk结构分析
查看>>