文章目录
前言配置切面ProxyFactoryProxyFactoryBean配置通知配置类定义目标类上下文类
参考
前言
编程AOP ,编程AOP ,为啥要了解编程AOP呢,因为实际上 我们用的AOP 操作都是基于编程来做的,只是 spring帮你做了一些操作,所以了解编程AOP 实际上对源码的理解还是有一定的作用的,也就明白了源码中 一些 为什么要这么做的理由
配置切面
如果是通过注解配置的话,其实 仅仅配置@Aspect 表示当前类是一个切面类,然后在方法上配置各种通知比如:@Before这里讲的是编程 AOP 所以就不多说 注解了
ProxyFactory
纯手动的AOP … spring 底层就是这样的,我基本就是直接复制 源码随便改改就好了…
public class ProxyFactoryContext implements ProxyFactoryContextImplements{
public static void main(String
[] args
) {
ProxyFactory proxyFactory
= new ProxyFactory();
proxyFactory
.setProxyTargetClass(false);
proxyFactory
.setInterfaces();
proxyFactory
.addAdvice((MethodBeforeAdvice
) (method
, args1
, target
) -> {
System
.out
.println("前置通知");
});
proxyFactory
.setTargetSource(new SingletonTargetSource(new ProxyFactoryContext()));
proxyFactory
.setExposeProxy(true);
ProxyFactoryContext proxy
= (ProxyFactoryContext
) proxyFactory
.getProxy();
proxy
.log();
System
.out
.println(proxy
.getClass().getName());
}
@Override
public void log() {
System
.out
.println("我是本体方法");
}
}
ProxyFactoryBean
可以从 spring 容器中获取
配置通知
@Component("beforeAdvice")
public class BeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method
, Object
[] objects
, Object o
) throws Throwable
{
System
.out
.println("before advice.");
}
}
配置类
@Configuration
public class ProxyFactoryConfig {
@Bean
public ProxyFactoryBean
proxyFactoryBean(ProxyFactoryBeanInterface beanInterface
) throws Exception
{
ProxyFactory factory
= new ProxyFactory();
ProxyFactoryBean factoryBean
= new ProxyFactoryBean();
factoryBean
.setTarget(beanInterface
);
factoryBean
.setProxyInterfaces(new Class[]{ProxyFactoryBeanInterface
.class});
factoryBean
.setInterceptorNames("beforeAdvice");
factoryBean
.addAdvice(new MethodBeforeAdvice (){
@Override
public void before(Method method
, Object
[] objects
, Object o
) throws Throwable
{
System
.out
.println("后置1");
}
});
factoryBean
.setProxyTargetClass(true);
factoryBean
.setExposeProxy(true);
return factoryBean
;
}
}
定义目标类
public interface ProxyFactoryBeanInterface {
public void add();
public void delete();
}
@Component
public class ProxyFactoryBeanInterfaceImpl implements ProxyFactoryBeanInterface {
@Override
public void add() {
System
.out
.println("++++++++++");
}
@Override
public void delete() {
System
.out
.println("--------------");
ProxyFactoryBeanInterface o
=(ProxyFactoryBeanInterface
) AopContext
.currentProxy();
o
.add();
}
}
上下文类
@ComponentScan
public class ProxyFactoryContext {
public static void main(String
[] args
) {
AnnotationConfigApplicationContext value
= new AnnotationConfigApplicationContext(ProxyFactoryContext
.class);
getThreadLocal(value
);
}
private static void getThreadLocal(AnnotationConfigApplicationContext value
) {
ProxyFactoryBeanInterface bean
= (ProxyFactoryBeanInterface
) value
.getBean("proxyFactoryBean");
bean
.delete();
System
.out
.println(bean
.getClass().getName());
}
private static void getBeanByFactoryBean(AnnotationConfigApplicationContext value
) {
ProxyFactoryBeanInterface bean
= (ProxyFactoryBeanInterface
) value
.getBean("proxyFactoryBean");
bean
.add();
System
.out
.println(bean
.getClass().getName());
}
private static void getProxyFactoryBean(AnnotationConfigApplicationContext value
) {
ProxyFactoryBean bean
= value
.getBean(ProxyFactoryBean
.class);
Object object
= bean
.getObject();
ProxyFactoryBeanInterface anInterface
= (ProxyFactoryBeanInterface
) object
;
anInterface
.add();
}
private static void getBean(AnnotationConfigApplicationContext value
) {
ProxyFactoryBeanInterface bean
= (ProxyFactoryBeanInterface
) value
.getBean("proxyFactoryBeanInterfaceImpl");
bean
.add();
}
}
参考
https://www.jianshu.com/p/b38b1a8cb0a4