aop的核心整个流程
1.AbstractAutoProxyCreator #postProcessAfterInitialization:初始化前的操作
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean != null ) {
Object cacheKey = getCacheKey(bean.getClass(), beanName);
if (!this .earlyProxyReferences.contains(cacheKey)) {
return wrapIfNecessary(bean, beanName, cacheKey);
}
}
return bean;
}
2.AbstractAutoProxyCreator #wrapIfNecessary:如果需要则包装bean(代理)
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
if (beanName != null && this .targetSourcedBeans.contains(beanName)) {
return bean;
}
if (Boolean .FALSE.equals(this .advisedBeans.get(cacheKey))) {
return bean;
}
if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
this .advisedBeans.put(cacheKey, Boolean .FALSE);
return bean;
}
Object [] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null );
if (specificInterceptors != DO_NOT_PROXY) {
this .advisedBeans.put(cacheKey, Boolean .TRUE);
Object proxy = createProxy(
bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
this .proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
this .advisedBeans.put(cacheKey, Boolean .FALSE);
return bean;
}
2.1.AnnotationAwareAspectJAutoProxyCreator#findCandidateAdvisors 查询所有的增强方法
@Override
protected List<Advisor> findCandidateAdvisors () {
List<Advisor> advisors = super .findCandidateAdvisors();
advisors.addAll(this .aspectJAdvisorsBuilder.buildAspectJAdvisors());
return advisors;
}
2.2.BeanFactoryAspectJAdvisorsBuilder #buildAspectJAdvisors提取所有的增强方法
public List<Advisor> buildAspectJAdvisors () {
List<String> aspectNames = this .aspectBeanNames;
if (aspectNames == null ) {
synchronized (this ) {
aspectNames = this .aspectBeanNames;
if (aspectNames == null ) {
List<Advisor> advisors = new LinkedList<>();
aspectNames = new LinkedList<>();
String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
this .beanFactory, Object.class, true , false );
for (String beanName : beanNames) {
if (!isEligibleBean(beanName)) {
continue ;
}
Class<?> beanType = this .beanFactory.getType(beanName);
if (beanType == null ) {
continue ;
}
if (this .advisorFactory.isAspect(beanType)) {
aspectNames.add(beanName);
AspectMetadata amd = new AspectMetadata(beanType, beanName);
if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) {
MetadataAwareAspectInstanceFactory factory =
new BeanFactoryAspectInstanceFactory(this .beanFactory, beanName);
List<Advisor> classAdvisors = this .advisorFactory.getAdvisors(factory);
if (this .beanFactory.isSingleton(beanName)) {
this .advisorsCache.put(beanName, classAdvisors);
}
else {
this .aspectFactoryCache.put(beanName, factory);
}
advisors.addAll(classAdvisors);
}
else {
if (this .beanFactory.isSingleton(beanName)) {
throw new IllegalArgumentException("Bean with name '" + beanName +
"' is a singleton, but aspect instantiation model is not singleton" );
}
MetadataAwareAspectInstanceFactory factory =
new PrototypeAspectInstanceFactory(this .beanFactory, beanName);
this .aspectFactoryCache.put(beanName, factory);
advisors.addAll(this .advisorFactory.getAdvisors(factory));
}
}
}
this .aspectBeanNames = aspectNames;
return advisors;
}
}
}
if (aspectNames.isEmpty()) {
return Collections.emptyList();
}
List<Advisor> advisors = new LinkedList<>();
for (String aspectName : aspectNames) {
List<Advisor> cachedAdvisors = this .advisorsCache.get (aspectName);
if (cachedAdvisors != null ) {
advisors.addAll(cachedAdvisors);
}
else {
MetadataAwareAspectInstanceFactory factory = this .aspectFactoryCache.get (aspectName);
advisors.addAll(this .advisorFactory.getAdvisors(factory));
}
}
return advisors;
}
2.3AbstractAutoProxyCreator#createProxy 创建代理
protected Object createProxy(
Class<?> beanClass, String beanName, Object [] specificInterceptors, TargetSource targetSource) {
if (this .beanFactory instanceof ConfigurableListableBeanFactory) {
AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this .beanFactory, beanName, beanClass);
}
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.copyFrom(this );
if (!proxyFactory.isProxyTargetClass()) {
if (shouldProxyTargetClass(beanClass, beanName)) {
proxyFactory.setProxyTargetClass(true );
}
else {
evaluateProxyInterfaces(beanClass, proxyFactory);
}
}
Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
for (Advisor advisor : advisors) {
proxyFactory.addAdvisor(advisor);
}
proxyFactory.setTargetSource(targetSource);
customizeProxyFactory(proxyFactory);
proxyFactory.setFrozen(this .freezeProxy);
if (advisorsPreFiltered()) {
proxyFactory.setPreFiltered(true );
}
return proxyFactory.getProxy(getProxyClassLoader());
}