java aop面向切面编程
2020-12-13 03:40
标签:div cat obj 技术 核心 hive handle 图片 static 最近一直在学java的spring boot,一直没有弄明白aop面向切面编程是什么意思。看到一篇文章写得很清楚,终于弄明白了,原来跟python的装饰器一样的效果。http://www.cnblogs.com/yanbincn/archive/2012/06/01/2530377.html 具体思想是:定义一个切面,在切面的纵向定义处理方法,处理完成之后,回到横向业务流。 AOP 在Spring框架中被作为核心组成部分之一,的确Spring将AOP发挥到很强大的功能。最常见的就是事务控制。工作之余,对于使用的工具,不免需要了解其所以然。学习了一下,写了些程序帮助理解。 AOP 主要是利用代理模式的技术来实现的。 静态代理(代码如下): 运行结果如下: No ProXY Test你好,我是AOP,我年龄是18 添加了一个新功能 以下是动态代理的代码: 运行结果如下: 切面之前执行 同样也添加了新功能 ,与python装饰器的功能大同小异。 java aop面向切面编程 标签:div cat obj 技术 核心 hive handle 图片 static 原文地址:https://www.cnblogs.com/linwenbin/p/11083463.htmlpackage com.practice.proxypattern;
public interface ITalk {
public void talk(String msg);
}
package com.practice.proxypattern;
public class PeopleTalk implements ITalk {
private String username;
private String age;
public PeopleTalk(String username,String age) {
this.username=username;
this.age=age;
}
public void talk(String msg) {
// TODO Auto-generated method stub
System.out.println(msg+"你好,我是"+username+",我年龄是"+age);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
package com.practice.proxypattern;
import java.security.PublicKey;
public class TalkProxy implements ITalk{
private ITalk talker;
public TalkProxy(ITalk talker) {
this.talker=talker;
}
public void talk(String msg) {
// TODO Auto-generated method stub
talker.talk(msg);
}
public void talk(String msg,String singname) {
talker.talk(msg);
sing(singname);
}
private void sing(String singname) {
System.out.println("唱歌:"+singname);
}
}
package com.practice.proxypattern;
public class ProxyPattern {
public static void main(String[] args) {
ITalk people=new PeopleTalk("AOP", "18");
people.talk("No ProXY Test");
System.out.println("-----------------------");
TalkProxy talker=new TalkProxy(people);
talker.talk("ProXY Test","代理");
}
}
-----------------------
ProXY Test你好,我是AOP,我年龄是18
唱歌:代理package com.practice.dynamicproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class DynamicProxy implements InvocationHandler {
//需要代理的目标类
private Object target;
public Object bind(Object target) {
this.target=target;
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
Object result=null;
System.out.println("切面之前执行");
result=method.invoke(target, args);
System.out.println("切面之后执行");
return result;
}
}
package com.practice.dynamicproxy;
import com.practice.proxypattern.ITalk;
import com.practice.proxypattern.PeopleTalk;
public class Test {
public static void main(String[] args) {
ITalk iTalk=(ITalk) new DynamicProxy().bind(new PeopleTalk("DynamicProxy","18"));
iTalk.talk("业务说明");
}
}
业务说明你好,我是DynamicProxy,我年龄是18
切面之后执行
上一篇:PHP5安装oci8模块