Summary:
Spring
Agent in the upper layer is divided into ProxyCreatorSupport
and ProxyProcessorSupport
the former is based on proxy factory, which is based on the post-processor, it can be considered that automatic post agent. When spring
the container needs to aop
be woven into a bean
time more, simply uses ProxyFacotryBean
will undoubtedly add a lot of workload (because each Bean
! Had to write a manual). So automatic proxy will play its role in the.
Spring automatically create proxy Category
In the interior, Spring use BeanPostProcessor
to make automatic generation of proxy. Class-based implementation of BeanPostProcessor autoproxy creator of the vessel in accordance with some examples of rules Bean
generated for the matching agent instance when the Bean. The proxy creator can be divided into three categories:
- Bean automatic proxy configuration name generated based on rules: allows automatic creator create proxy agent instance for a particular configuration group Bean name, class implemented
BeanNameAutoProxyCreator
- Based on
Advisor
its Advisor on all containers are scanned automatically create matching proxy mechanism, which automatically cut applied to the matching Bean, the implementation class isDefaultAdvisorAutoProxyCreator
(it also supports prefix matching) - Based on the Bean
AspectJ
annotated automatic proxy generator: create Bean cut the AspectJ annotation contains automatic proxy instance, implementation class isAnnotationAwareAspectJAutoProxyCreator
, it is our@EnableAspectJAutoProxy
import, which is our current most widely used way ~
BeanNameAutoProxyCreator
package com.github.dqqzj.springboot.aop;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @author qinzhongjian
* @date created in 2019-08-25 09:43
* @description: TODO
* @since JDK 1.8.0_212-b10
*/
@Component
public class MyBeanNameAutoProxyCreator extends BeanNameAutoProxyCreator {
@PostConstruct
public void init() {
super.setBeanNames("aopService", "abstractAutoProxyCreatorService");
super.setInterceptorNames("myMethodBeforeAdvice");
}
}
If you want to use their registered @Bean
place of @EnableAspectJAutoProxy
default to automatically register your creator AnnotationAwareAspectJAutoProxyCreator
, you can sign up to a Bean Bean name as follows:
// 手动注册一个自动代理创建器,且名字务必叫AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME
@Bean(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME)
public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {
...
}
AbstractAutoProxyCreator
public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware {
...
}
AbstractAutoProxyCreator
It is an abstract realization of the auto proxy creator. Most importantly, it implements the SmartInstantiationAwareBeanPostProcessor
interface, will be involved in the Spring IoC
container Bean instantiation process, AbstractAutowireCapableBeanFactory
there is a piece of code like this
try {
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
}
But generally it will not take effect, because this resolveBeforeInstantiation
is only for self-definition targetsource
, because the custom targetsource
is not so sure spring of bean does not need to follow a series of examples of initialization. It can be done directly in resolveBeforeInstantiation proxy
. Simple Well, this code can be ignored, developers generally use less.
How to make resolveBeforeInstantiation return directly bean?
package com.github.dqqzj.springboot.aop;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
/**
* @author qinzhongjian
* @date created in 2019-08-25 11:35
* @description: TODO
* @since JDK 1.8.0_212-b10
*/
public class AopServiceInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
if (beanClass.isInstance(AopService.class)) {
return new AopService();
}
return null;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
This is the spring
first time the post-processor, if so directly returned, out of the equivalent of IOC
the life cycle of the same dependency injection, filling and other properties which are not treated, it must be noted that when using, better not to use this feature.
In the process of initializing the bean in the follow-up there are two particularly important post-treatment process, the impact of asynchronous cycle of dependence and even annotations and notes things have more or less, follow-up will continue to analyze them.
summary:
SpringAOP
Should be avoided to create their own AutoProxyCreator
, internal mechanisms and their complexity will inevitably arise other unusual problems because the problem is not thought to share the above content rarely, because I feel familiar with this most crucial architects, various subclasses While achieve very different, but all you want to keep in mind or are familiar with it is not realistic, only familiar with his design in order to meet the problem can be easily solved turn source.