📄 abstractautoproxycreator.java
字号:
}
return null;
}
public Object postProcessBeforeInitialization(Object bean, String beanName) {
return bean;
}
/**
* Create a proxy with the configured interceptors if the bean is
* identified as one to proxy by the subclass.
* @see #getAdvicesAndAdvisorsForBean
*/
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (isInfrastructureClass(bean.getClass(), beanName)) {
return bean;
}
// Create proxy if we have advice.
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
if (specificInterceptors != DO_NOT_PROXY) {
return createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
}
return bean;
}
/**
* Return whether the given bean class and bean name represents an
* infrastructure class that should never be proxied.
* <p>Default implementation considers Advisors, MethodInterceptors
* and AbstractAutoProxyCreators as infrastructure classes, and
* consults the <code>shouldSkip</coide> method.
* @param beanClass the class of the bean
* @param beanName the name of the bean
* @return whether the bean represents an infrastructure class
* @see org.springframework.aop.Advisor
* @see org.aopalliance.intercept.MethodInterceptor
* @see #shouldSkip
*/
protected boolean isInfrastructureClass(Class beanClass, String beanName) {
boolean retVal = Advisor.class.isAssignableFrom(beanClass) ||
MethodInterceptor.class.isAssignableFrom(beanClass) ||
AbstractAutoProxyCreator.class.isAssignableFrom(beanClass) ||
shouldSkip(beanClass, beanName);
if (retVal && logger.isDebugEnabled()) {
logger.debug("Did not attempt to autoproxy infrastructure class [" + beanClass.getName() + "]");
}
return retVal;
}
/**
* Subclasses should override this method to return true if this
* bean should not be considered for auto-proxying by this post processor.
* Sometimes we need to be able to avoid this happening if it will lead to
* a circular reference. This implementation returns <code>false</code>.
* @param beanClass the class of the bean
* @param beanName the name of the bean
*/
protected boolean shouldSkip(Class beanClass, String beanName) {
return false;
}
/**
* Create a target source for bean instances. Uses any TargetSourceCreators if set.
* Returns <code>null</code> if no custom TargetSource should be used.
* <p>This implementation uses the "customTargetSourceCreators" property.
* Subclasses can override this method to use a different mechanism.
* @param beanClass the class of the bean to create a TargetSource for
* @param beanName the name of the bean
* @return a TargetSource for this bean
* @see #setCustomTargetSourceCreators
*/
protected TargetSource getCustomTargetSource(Class beanClass, String beanName) {
// We can't create fancy target sources for directly registered singletons.
if (this.beanFactory != null && this.beanFactory.containsBean(beanName)) {
if (logger.isDebugEnabled()) {
logger.debug("Checking for custom TargetSource for bean with name '" + beanName + "'");
}
if (this.customTargetSourceCreators != null) {
for (int i = 0; i < this.customTargetSourceCreators.length; i++) {
TargetSourceCreator tsc = this.customTargetSourceCreators[i];
TargetSource ts = tsc.getTargetSource(beanClass, beanName);
if (ts != null) {
// Found a matching TargetSource.
if (logger.isDebugEnabled()) {
logger.debug("TargetSourceCreator [" + tsc +
" found custom TargetSource for bean with name '" + beanName + "'");
}
return ts;
}
}
}
}
// No custom TargetSource found.
return null;
}
/**
* Create an AOP proxy for the given bean.
* @param beanClass the class of the bean
* @param beanName the name of the bean
* @param specificInterceptors the set of interceptors that is
* specific to this bean (may be empty, but not null)
* @param targetSource the TargetSource for the proxy,
* already pre-configured to access the bean
* @return the AOP proxy for the bean
*/
protected Object createProxy(
Class beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) {
// handle prototypes correctly
Advisor[] commonInterceptors = resolveInterceptorNames();
List allInterceptors = new ArrayList();
if (specificInterceptors != null) {
allInterceptors.addAll(Arrays.asList(specificInterceptors));
if (commonInterceptors != null) {
if (this.applyCommonInterceptorsFirst) {
allInterceptors.addAll(0, Arrays.asList(commonInterceptors));
}
else {
allInterceptors.addAll(Arrays.asList(commonInterceptors));
}
}
}
if (logger.isDebugEnabled()) {
int nrOfCommonInterceptors = commonInterceptors != null ? commonInterceptors.length : 0;
int nrOfSpecificInterceptors = specificInterceptors != null ? specificInterceptors.length : 0;
logger.debug(
"Creating implicit proxy for bean '" + beanName + "' with " + nrOfCommonInterceptors +
" common interceptors and " + nrOfSpecificInterceptors + " specific interceptors");
}
ProxyFactory proxyFactory = new ProxyFactory();
// Copy our properties (proxyTargetClass) inherited from ProxyConfig.
proxyFactory.copyFrom(this);
if (!isProxyTargetClass()) {
// Must allow for introductions; can't just set interfaces to
// the target's interfaces only.
Class[] targetsInterfaces = ClassUtils.getAllInterfacesForClass(beanClass);
for (int i = 0; i < targetsInterfaces.length; i++) {
proxyFactory.addInterface(targetsInterfaces[i]);
}
}
for (Iterator it = allInterceptors.iterator(); it.hasNext();) {
Advisor advisor = this.advisorAdapterRegistry.wrap(it.next());
proxyFactory.addAdvisor(advisor);
}
proxyFactory.setTargetSource(targetSource);
customizeProxyFactory(proxyFactory);
proxyFactory.setFrozen(this.freezeProxy);
return proxyFactory.getProxy();
}
/**
* Resolves the specified interceptor names to Advisor objects.
* @see #setInterceptorNames
*/
private Advisor[] resolveInterceptorNames() {
Advisor[] advisors = new Advisor[this.interceptorNames.length];
for (int i = 0; i < this.interceptorNames.length; i++) {
Object next = this.beanFactory.getBean(this.interceptorNames[i]);
advisors[i] = this.advisorAdapterRegistry.wrap(next);
}
return advisors;
}
/**
* Subclasses may choose to implement this: for example,
* to change the interfaces exposed.
* <p>Default implementation is emty.
* @param proxyFactory ProxyFactory that is already configured with
* TargetSource and interfaces and will be used to create the proxy
* immediably after this method returns
*/
protected void customizeProxyFactory(ProxyFactory proxyFactory) {
}
/**
* Return whether the given bean is to be proxied, what additional
* advices (e.g. AOP Alliance interceptors) and advisors to apply.
* <p>The previous name of this method was "getInterceptorAndAdvisorForBean".
* It has been renamed in the course of general terminology clarification
* in Spring 1.1. An AOP Alliance Interceptor is just a special form of
* Advice, so the generic Advice term is preferred now.
* <p>The third parameter, customTargetSource, is new in Spring 1.1;
* add it to existing implementations of this method.
* @param beanClass the class of the bean to advise
* @param beanName the name of the bean
* @param customTargetSource targetSource returned by getTargetSource() method:
* may be ignored. Will be <code>null</code> unless a custom target source is in use.
* @return an array of additional interceptors for the particular bean;
* or an empty array if no additional interceptors but just the common ones;
* or <code>null</code> if no proxy at all, not even with the common interceptors.
* See constants DO_NOT_PROXY and PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS.
* @throws BeansException in case of errors
* @see #postProcessAfterInitialization
* @see #DO_NOT_PROXY
* @see #PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS
*/
protected abstract Object[] getAdvicesAndAdvisorsForBean(
Class beanClass, String beanName, TargetSource customTargetSource) throws BeansException;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -