Advised
在Spring框架中,Advisor接口是一个核心概念,用于表示一个切面的配置信息,它结合了一个或多个Advice(通知)和一个或多个Pointcut(切点)。Advisor的主要作用是将通知与切点关联起来,以便在匹配切点的方法执行时能够触发相应的通知。
源码
/* 简单来说,这个接口定义了一个标准,用于描述和操作AOP代理的配置。
* 任何需要创建或修改AOP代理配置的类都应该实现这个接口。
* 通过这个接口,我们可以动态地添加、删除或修改AOP代理的拦截器、通知和顾问,以及指定哪些接口需要被代理。
* 在Spring框架中,一旦你获得了一个AOP代理对象,你可以将它转换为这个接口,从而能够直接操作其AOP配置。
*
* 从Spring Framework 5.0开始,Advised接口已经被标记为@Deprecated,取而代之的是ProxyConfig类和其他相关类,
* 用于配置和管理AOP代理。因此,在新的Spring项目中,建议使用新的API来处理AOP代理的配置和操作。
*/
public interface Advised extends TargetClassAware {
/**
* Return whether the Advised configuration is frozen,
* in which case no advice changes can be made.
* 冻结配置,不允许更改 比如代理对象被创建之后
*/
boolean isFrozen();
/**
* Are we proxying the full target class instead of specified interfaces?
* 表示是否应该代理目标类而不是接口 true 使用CGLIB false 使用JDK动态代理
*
*/
boolean isProxyTargetClass();
/**
* Return the interfaces proxied by the AOP proxy.
* <p>Will not include the target class, which may also be proxied.
*/
Class<?>[] getProxiedInterfaces();
/**
* Determine whether the given interface is proxied.
* @param intf the interface to check
*/
boolean isInterfaceProxied(Class<?> intf);
/**
* Change the {@code TargetSource} used by this {@code Advised} object.
* <p>Only works if the configuration isn't {@linkplain #isFrozen frozen}.
* @param targetSource new TargetSource to use
* 设置被代理对象
*/
void setTargetSource(TargetSource targetSource);
/**
* Return the {@code TargetSource} used by this {@code Advised} object.
* 被代理的对象
*/
TargetSource getTargetSource();
/**
* Set whether the proxy should be exposed by the AOP framework as a
* {@link ThreadLocal} for retrieval via the {@link AopContext} class.
* <p>It can be necessary to expose the proxy if an advised object needs
* to invoke a method on itself with advice applied. Otherwise, if an
* advised object invokes a method on {@code this}, no advice will be applied.
* <p>Default is {@code false}, for optimal performance.
* 是否暴露代理对象
*/
void setExposeProxy(boolean exposeProxy);
/**
* Return whether the factory should expose the proxy as a {@link ThreadLocal}.
* <p>It can be necessary to expose the proxy if an advised object needs
* to invoke a method on itself with advice applied. Otherwise, if an
* advised object invokes a method on {@code this}, no advice will be applied.
* <p>Getting the proxy is analogous to an EJB calling {@code getEJBObject()}.
* @see AopContext
*/
boolean isExposeProxy();
/**
* Set whether this proxy configuration is pre-filtered so that it only
* contains applicable advisors (matching this proxy's target class).
* <p>Default is "false". Set this to "true" if the advisors have been
* pre-filtered already, meaning that the ClassFilter check can be skipped
* when building the actual advisor chain for proxy invocations.
* @see org.springframework.aop.ClassFilter
*/
void setPreFiltered(boolean preFiltered);
/**
* Return whether this proxy configuration is pre-filtered so that it only
* contains applicable advisors (matching this proxy's target class).
*/
boolean isPreFiltered();
/**
* Return the advisors applying to this proxy.
* @return a list of Advisors applying to this proxy (never {@code null})
* 获取切面集合
*/
Advisor[] getAdvisors();
/**
* Add an advisor at the end of the advisor chain.
* <p>The Advisor may be an {@link org.springframework.aop.IntroductionAdvisor},
* in which new interfaces will be available when a proxy is next obtained
* from the relevant factory.
* @param advisor the advisor to add to the end of the chain
* @throws AopConfigException in case of invalid advice
* 添加一个切面
*/
void addAdvisor(Advisor advisor) throws AopConfigException;
/**
* Add an Advisor at the specified position in the chain.
* @param advisor the advisor to add at the specified position in the chain
* @param pos position in chain (0 is head). Must be valid.
* @throws AopConfigException in case of invalid advice
* 再指定顺序添加一个切面
*/
void addAdvisor(int pos, Advisor advisor) throws AopConfigException;
/**
* Remove the given advisor.
* @param advisor the advisor to remove
* @return {@code true} if the advisor was removed; {@code false}
* if the advisor was not found and hence could not be removed
* 移除一个切面
*/
boolean removeAdvisor(Advisor advisor);
/**
* Remove the advisor at the given index.
* @param index index of advisor to remove
* @throws AopConfigException if the index is invalid
* 根据下标移除切面
*/
void removeAdvisor(int index) throws AopConfigException;
/**
* Return the index (from 0) of the given advisor,
* or -1 if no such advisor applies to this proxy.
* <p>The return value of this method can be used to index into the advisors array.
* @param advisor the advisor to search for
* @return index from 0 of this advisor, or -1 if there's no such advisor
* 获取一个切面的下标
*/
int indexOf(Advisor advisor);
/**
* Replace the given advisor.
* <p><b>Note:</b> If the advisor is an {@link org.springframework.aop.IntroductionAdvisor}
* and the replacement is not or implements different interfaces, the proxy will need
* to be re-obtained or the old interfaces won't be supported and the new interface
* won't be implemented.
* @param a the advisor to replace
* @param b the advisor to replace it with
* @return whether it was replaced. If the advisor wasn't found in the
* list of advisors, this method returns {@code false} and does nothing.
* @throws AopConfigException in case of invalid advice
* 使用b切面替换a切面
*/
boolean replaceAdvisor(Advisor a, Advisor b) throws AopConfigException;
/**
* Add the given AOP Alliance advice to the tail of the advice (interceptor) chain.
* <p>This will be wrapped in a DefaultPointcutAdvisor with a pointcut that always
* applies, and returned from the {@code getAdvisors()} method in this wrapped form.
* <p>Note that the given advice will apply to all invocations on the proxy,
* even to the {@code toString()} method! Use appropriate advice implementations
* or specify appropriate pointcuts to apply to a narrower set of methods.
* @param advice advice to add to the tail of the chain
* @throws AopConfigException in case of invalid advice
* @see #addAdvice(int, Advice)
* @see org.springframework.aop.support.DefaultPointcutAdvisor
* 添加通知方法
*/
void addAdvice(Advice advice) throws AopConfigException;
/**
* Add the given AOP Alliance Advice at the specified position in the advice chain.
* <p>This will be wrapped in a {@link org.springframework.aop.support.DefaultPointcutAdvisor}
* with a pointcut that always applies, and returned from the {@link #getAdvisors()}
* method in this wrapped form.
* <p>Note: The given advice will apply to all invocations on the proxy,
* even to the {@code toString()} method! Use appropriate advice implementations
* or specify appropriate pointcuts to apply to a narrower set of methods.
* @param pos index from 0 (head)
* @param advice advice to add at the specified position in the advice chain
* @throws AopConfigException in case of invalid advice
* 在指定位置添加通知方法
*/
void addAdvice(int pos, Advice advice) throws AopConfigException;
/**
* Remove the Advisor containing the given advice.
* @param advice the advice to remove
* @return {@code true} of the advice was found and removed;
* {@code false} if there was no such advice
* 移除通知方法
*/
boolean removeAdvice(Advice advice);
/**
* Return the index (from 0) of the given AOP Alliance Advice,
* or -1 if no such advice is an advice for this proxy.
* <p>The return value of this method can be used to index into
* the advisors array.
* @param advice the AOP Alliance advice to search for
* @return index from 0 of this advice, or -1 if there's no such advice
* 获取这个通知方法的下标
*/
int indexOf(Advice advice);
/**
* As {@code toString()} will normally be delegated to the target,
* this returns the equivalent for the AOP proxy.
* @return a string description of the proxy configuration
* 将配置信息调用toString方法打印出来
*/
String toProxyConfigString();
}
示例
<aop:config>
<aop:advisor pointcut="execution(* com.example.myapp.MyService.*(..))" advice-ref="myAdvice"/>
</aop:config>
<bean id="myAdvice" class="com.example.myapp.MyAdvice"/>
ProxyConfig
ProxyConfig 类在 Spring 框架中用于配置 AOP 代理的相关属性。它是 Spring AOP 的一个核心类,提供了构建和管理代理对象所需的各种设置。
源码
public class ProxyConfig implements Serializable {
/** use serialVersionUID from Spring 1.2 for interoperability. */
private static final long serialVersionUID = -8409359707199703185L;
//表示是否应该代理目标类而不是接口 true 使用CGLIB false 使用JDK动态代理
private boolean proxyTargetClass = false;
//表示是否应该优化代理类
private boolean optimize = false;
boolean opaque = false;
//表示是否应该将代理对象暴露给被代理的目标对象。这通常用于在目标对象内部通过 AopContext 访问代理对象
boolean exposeProxy = false;
//表示配置是否已经被冻结。一旦冻结,就不能再修改配置。
private boolean frozen = false;
}
使用场景
通常,你不会直接实例化或修改 ProxyConfig 对象。相反,Spring AOP 的其他组件(如 ProxyFactoryBean 或 @Aspect 注解的处理器)会在内部使用它来构建和管理 AOP 代理。当你需要自定义代理行为时(例如,通过编程方式创建代理),你可能会直接与 ProxyConfig 交互。
总结
ProxyConfig 类是 Spring AOP 中用于配置和管理代理对象的核心类。它提供了设置目标对象、拦截器、代理类型等属性的方法,并允许你控制代理的创建和优化过程。通过理解和使用 ProxyConfig,你可以更灵活地控制 Spring AOP 代理的行为。