public interface ITask {
void excute();
}
public class MockTask implements ITask{
@Override
public void excute() {
System.out.println("task excuted!");
}
}
public class PerformanceMethodInterceptor implements MethodInterceptor {
@Nullable
@Override
public Object invoke(@Nonnull MethodInvocation methodInvocation) throws Throwable {
long start = System.currentTimeMillis();
System.out.println("方法执行前...");
Thread.sleep(1000);
Object proceed = methodInvocation.proceed();
Thread.sleep(1000);
System.out.println("方法执行后");
long end = System.currentTimeMillis();
System.out.println("方法执行总耗时: "+String.valueOf(end-start));
return proceed;
}
}
MockTask task=new MockTask();
ProxyFactory weaver=new ProxyFactory(task);
weaver.setInterfaces(ITask.class);
NameMatchMethodPointcutAdvisor advisor=new NameMatchMethodPointcutAdvisor();
advisor.setMappedName("excute");
advisor.setAdvice(new PerformanceMethodInterceptor());
weaver.addAdvisor(advisor);
ITask proxyObject= (ITask) weaver.getProxy();
proxyObject.excute();
MockTask task=new MockTask();
ProxyFactory weaver=new ProxyFactory(task);
NameMatchMethodPointcutAdvisor advisor=new NameMatchMethodPointcutAdvisor();
advisor.setMappedName("excute");
advisor.setAdvice(new PerformanceMethodInterceptor());
weaver.addAdvisor(advisor);
ITask proxyObject= (ITask) weaver.getProxy();
proxyObject.excute();
public class MockTask {
public void excute() {
System.out.println("task excuted!");
}
}
MockTask task=new MockTask();
ProxyFactory weaver=new ProxyFactory(task);
NameMatchMethodPointcutAdvisor advisor=new NameMatchMethodPointcutAdvisor();
advisor.setMappedName("excute");
advisor.setAdvice(new PerformanceMethodInterceptor());
weaver.addAdvisor(advisor);
MockTask proxy = (MockTask)weaver.getProxy();
proxy.excute();
System.out.println(proxy.getClass());
public interface AopProxy {
Object getProxy();
Object getProxy(@Nullable ClassLoader var1);
}
public interface AopProxyFactory {
AopProxy createAopProxy(AdvisedSupport var1) throws AopConfigException;
}
public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
public DefaultAopProxyFactory() {
}
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
if (NativeDetector.inNativeImage() || !config.isOptimize() && !config.isProxyTargetClass() && !this.hasNoUserSuppliedProxyInterfaces(config)) {
return new JdkDynamicAopProxy(config);
} else {
Class<?> targetClass = config.getTargetClass();
if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class: Either an interface or a target is required for proxy creation.");
} else {
return (AopProxy)(!targetClass.isInterface() && !Proxy.isProxyClass(targetClass) ? new ObjenesisCglibAopProxy(config) : new JdkDynamicAopProxy(config));
}
}
}
private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) {
Class<?>[] ifcs = config.getProxiedInterfaces();
return ifcs.length == 0 || ifcs.length == 1 && SpringProxy.class.isAssignableFrom(ifcs[0]);
}
}
public class ProxyConfig implements Serializable {
private static final long serialVersionUID = -8409359707199703185L;
private boolean proxyTargetClass = false;
private boolean optimize = false;
boolean opaque = false;
boolean exposeProxy = false;
private boolean frozen = false;
....
}
public interface Advised extends TargetClassAware {
boolean isFrozen();
boolean isProxyTargetClass();
Class<?>[] getProxiedInterfaces();
boolean isInterfaceProxied(Class<?> var1);
void setTargetSource(TargetSource var1);
TargetSource getTargetSource();
void setExposeProxy(boolean var1);
boolean isExposeProxy();
void setPreFiltered(boolean var1);
boolean isPreFiltered();
Advisor[] getAdvisors();
default int getAdvisorCount() {
return this.getAdvisors().length;
}
void addAdvisor(Advisor var1) throws AopConfigException;
void addAdvisor(int var1, Advisor var2) throws AopConfigException;
boolean removeAdvisor(Advisor var1);
void removeAdvisor(int var1) throws AopConfigException;
int indexOf(Advisor var1);
boolean replaceAdvisor(Advisor var1, Advisor var2) throws AopConfigException;
void addAdvice(Advice var1) throws AopConfigException;
void addAdvice(int var1, Advice var2) throws AopConfigException;
boolean removeAdvice(Advice var1);
int indexOf(Advice var1);
String toProxyConfigString();
}
上面的图只是画了一个大概的联系,大家再结合上面的文件好好理解一下
ProxyCreatorSupport内部拥有一个AopProxyFactory的对象实例,但是获取AopProxy的时候,是通过AopProxyFactory 的createAopProxy方法中传入自身实例ProxyCreatorSupport,来得到一个指定的AopProxy的,通过AopProxy 我们就可以获取到被代理后的对象
之说以传入自身,是因为ProxyCreatorSupport实现了AdvisedSupport,AdvisedSupport又实现了Advised接口,可以因此来获取到被代理对象实例和横切逻辑
public class ProxyCreatorSupport extends AdvisedSupport {
private AopProxyFactory aopProxyFactory;
private final List<AdvisedSupportListener> listeners = new ArrayList();
private boolean active = false;
public ProxyCreatorSupport() {
this.aopProxyFactory = new DefaultAopProxyFactory();
}
public ProxyCreatorSupport(AopProxyFactory aopProxyFactory) {
Assert.notNull(aopProxyFactory, "AopProxyFactory must not be null");
this.aopProxyFactory = aopProxyFactory;
}
....
protected final synchronized AopProxy createAopProxy() {
if (!this.active) {
this.activate();
}
return this.getAopProxyFactory().createAopProxy(this);
}
@Nullable
public Object getObject() throws BeansException {
this.initializeAdvisorChain();
if (this.isSingleton()) {
return this.getSingletonInstance();
} else {
if (this.targetName == null) {
this.logger.info("Using non-singleton proxies with singleton targets is often undesirable. Enable prototype proxies by setting the 'targetName' property.");
}
return this.newPrototypeInstance();
}
}
需要明确一点,既然我们使用了自动代理来自动为容器中符合条件的bean生成代理对象,那么我们只需要在配置文件中指定切入点和advice即可了
public interface TargetSource extends TargetClassAware {
@Nullable
Class<?> getTargetClass();
boolean isStatic();
@Nullable
Object getTarget() throws Exception;
void releaseTarget(Object var1) throws Exception;
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://cjdhy.blog.csdn.net/article/details/123695776
内容来源于网络,如有侵权,请联系作者删除!