public interface TargetClassAware {
@Nullable
Class> getTargetClass();
}
Advised—代理类配置信息封装
Spring官方英文注释:
Interface to be implemented by classes that hold the configuration of a factory of AOP proxies. This configuration includes the Interceptors and other advice, Advisors, and the proxied interfaces.
Any AOP proxy obtained from Spring can be cast to this interface to allow manipulation of its AOP advice.
public class AdvisedSupport extends ProxyConfig implements Advised {
private static final long serialVersionUID = 2651364800145442165L;
public static final TargetSource EMPTY_TARGET_SOURCE = EmptyTargetSource.INSTANCE;
TargetSource targetSource = EMPTY_TARGET_SOURCE;
private boolean preFiltered = false;
AdvisorChainFactory advisorChainFactory = new DefaultAdvisorChainFactory();
private transient Map> methodCache;
private List> interfaces = new ArrayList<>();
private List advisors = new ArrayList<>();
public List
AdvisorChainFactory–获取方法关联的拦截器链
public interface AdvisorChainFactory {
List getInterceptorsAndDynamicInterceptionAdvice(Advised config, Method method, @Nullable Class> targetClass);
}
该类只有一个默认实现
DefaultAdvisorChainFactory—唯一的默认实现
A simple but definitive way of working out an advice chain for a Method, given an Advised object. Always rebuilds each advice chain; caching can be provided by subclasses.
public class DefaultAdvisorChainFactory implements AdvisorChainFactory, Serializable {
@Override
public List getInterceptorsAndDynamicInterceptionAdvice(
Advised config, Method method, @Nullable Class> targetClass) {
// This is somewhat tricky... We have to process introductions first,
// but we need to preserve order in the ultimate list.
//AdvisorAdapterRegistry负责将advisor转换为MethodInterceptor
//GlobalAdvisorAdapterRegistry获取到的是全局共享的AdvisorAdapterRegistry,下面会讲到
AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
//获取应用到当前代理类上的所有增强器
Advisor[] advisors = config.getAdvisors();
List interceptorList = new ArrayList<>(advisors.length);
Class> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
Boolean hasIntroductions = null;
for (Advisor advisor : advisors) {
//处理PointcutAdvisor
if (advisor instanceof PointcutAdvisor) {
// Add it conditionally.
PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
boolean match;
if (mm instanceof IntroductionAwareMethodMatcher) {
if (hasIntroductions == null) {
hasIntroductions = hasMatchingIntroductions(advisors, actualClass);
}
match = ((IntroductionAwareMethodMatcher) mm).matches(method, actualClass, hasIntroductions);
}
else {
match = mm.matches(method, actualClass);
}
if (match) {
//利用registry将advisor转换为MethodInterceptor[]
MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
if (mm.isRuntime()) {
// Creating a new object instance in the getInterceptors() method
// isn't a problem as we normally cache created chains.
for (MethodInterceptor interceptor : interceptors) {
interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptor, mm));
}
}
else {
interceptorList.addAll(Arrays.asList(interceptors));
}
}
}
}
//处理IntroductionAdvisor
else if (advisor instanceof IntroductionAdvisor) {
IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) {
//利用registry将advisor转换为MethodInterceptor[]
Interceptor[] interceptors = registry.getInterceptors(advisor);
interceptorList.addAll(Arrays.asList(interceptors));
}
}
//处理其他advisor
else {
//利用registry将advisor转换为MethodInterceptor[]
Interceptor[] interceptors = registry.getInterceptors(advisor);
interceptorList.addAll(Arrays.asList(interceptors));
}
}
return interceptorList;
}
private static boolean hasMatchingIntroductions(Advisor[] advisors, Class> actualClass) {
for (Advisor advisor : advisors) {
if (advisor instanceof IntroductionAdvisor) {
IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
//IntroductionAdvisor只实现了类级别的过滤
if (ia.getClassFilter().matches(actualClass)) {
return true;
}
}
}
return false;
}
}
public class DefaultAdvisorAdapterRegistry implements AdvisorAdapterRegistry, Serializable {
private final List adapters = new ArrayList<>(3);
public DefaultAdvisorAdapterRegistry() {
registerAdvisorAdapter(new MethodBeforeAdviceAdapter());
registerAdvisorAdapter(new AfterReturningAdviceAdapter());
registerAdvisorAdapter(new ThrowsAdviceAdapter());
}
//advice适配为advisor后返回
@Override
public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException {
if (adviceObject instanceof Advisor) {
return (Advisor) adviceObject;
}
if (!(adviceObject instanceof Advice)) {
throw new UnknownAdviceTypeException(adviceObject);
}
Advice advice = (Advice) adviceObject;
if (advice instanceof MethodInterceptor) {
// So well-known it doesn't even need an adapter.
return new DefaultPointcutAdvisor(advice);
}
for (AdvisorAdapter adapter : this.adapters) {
// Check that it is supported.
if (adapter.supportsAdvice(advice)) {
return new DefaultPointcutAdvisor(advice);
}
}
throw new UnknownAdviceTypeException(advice);
}
//advisor适配为MethodInterceptor[]
@Override
public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
List interceptors = new ArrayList<>(3);
Advice advice = advisor.getAdvice();
//如果本身就是MethodInterceptor,那就不需要进行适配,直接加入集合
if (advice instanceof MethodInterceptor) {
interceptors.add((MethodInterceptor) advice);
}
//AdvisorAdapter转换的都是本身并不是MethodInterceptor的advisor
for (AdvisorAdapter adapter : this.adapters) {
if (adapter.supportsAdvice(advice)) {
interceptors.add(adapter.getInterceptor(advisor));
}
}
if (interceptors.isEmpty()) {
throw new UnknownAdviceTypeException(advisor.getAdvice());
}
//一个advisor转换为一个MethodInterceptor
return interceptors.toArray(new MethodInterceptor[0]);
}
@Override
public void registerAdvisorAdapter(AdvisorAdapter adapter) {
this.adapters.add(adapter);
}
}
// 它实现了Order接口哦~~~~支持排序的
public interface AspectInstanceFactory extends Ordered {
//Create an instance of this factory's aspect.
Object getAspectInstance();
//Expose the aspect class loader that this factory uses.
@Nullable
ClassLoader getAspectClassLoader();
}
public class BeanFactoryAspectInstanceFactory implements MetadataAwareAspectInstanceFactory, Serializable {
// 持有对Bean工厂的引用
private final BeanFactory beanFactory;
// 需要处理的名称
private final String name;
private final AspectMetadata aspectMetadata;
// 传了Name,type可议不传,内部判断出来
public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name) {
this(beanFactory, name, null);
}
public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String name, @Nullable Class> type) {
this.beanFactory = beanFactory;
this.name = name;
Class> resolvedType = type;
// 若没传type,就去Bean工厂里看看它的Type是啥 type不能为null~~~~
if (type == null) {
resolvedType = beanFactory.getType(name);
Assert.notNull(resolvedType, "Unresolvable bean type - explicitly specify the aspect class");
}
// 包装成切面元数据类
this.aspectMetadata = new AspectMetadata(resolvedType, name);
}
// 此处:切面实例 是从Bean工厂里获取的 需要注意
// 若是多例的,请注意Scope的值
@Override
public Object getAspectInstance() {
return this.beanFactory.getBean(this.name);
}
@Override
@Nullable
public ClassLoader getAspectClassLoader() {
return (this.beanFactory instanceof ConfigurableBeanFactory ?
((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader() :
ClassUtils.getDefaultClassLoader());
}
@Override
public AspectMetadata getAspectMetadata() {
return this.aspectMetadata;
}
@Override
@Nullable
public Object getAspectCreationMutex() {
if (this.beanFactory.isSingleton(this.name)) {
// Rely on singleton semantics provided by the factory -> no local lock.
return null;
}
else if (this.beanFactory instanceof ConfigurableBeanFactory) {
// No singleton guarantees from the factory -> let's lock locally but
// reuse the factory's singleton lock, just in case a lazy dependency
// of our advice bean happens to trigger the singleton lock implicitly...
return ((ConfigurableBeanFactory) this.beanFactory).getSingletonMutex();
}
else {
return this;
}
}
@Override
public int getOrder() {
Class> type = this.beanFactory.getType(this.name);
if (type != null) {
if (Ordered.class.isAssignableFrom(type) && this.beanFactory.isSingleton(this.name)) {
return ((Ordered) this.beanFactory.getBean(this.name)).getOrder();
}
// 若没实现接口,就拿注解的值
return OrderUtils.getOrder(type, Ordered.LOWEST_PRECEDENCE);
}
return Ordered.LOWEST_PRECEDENCE;
}
}
PrototypeAspectInstanceFactory----多例专用的工厂
public class PrototypeAspectInstanceFactory extends BeanFactoryAspectInstanceFactory implements Serializable {
public PrototypeAspectInstanceFactory(BeanFactory beanFactory, String name) {
super(beanFactory, name);
// 若不是多例,直接报错了
if (!beanFactory.isPrototype(name)) {
throw new IllegalArgumentException(
"Cannot use PrototypeAspectInstanceFactory with bean named '" + name + "': not a prototype");
}
}
}