📄 abstractautoproxycreator.java
字号:
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aop.framework.autoproxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.Advisor;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.ProxyConfig;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.framework.adapter.AdvisorAdapterRegistry;
import org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry;
import org.springframework.aop.target.SingletonTargetSource;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.util.ClassUtils;
/**
* BeanPostProcessor implementation that wraps a group of beans with AOP proxies
* that delegate to the given interceptors before invoking the bean itself.
*
* <p>This class distinguishes between "common" interceptors: shared for all proxies it
* creates, and "specific" interceptors: unique per bean instance. There need not
* be any common interceptors. If there are, they are set using the interceptorNames
* property. As with ProxyFactoryBean, interceptors names in the current factory
* are used rather than bean references to allow correct handling of prototype
* advisors and interceptors: for example, to support stateful mixins.
* Any advice type is supported for "interceptorNames" entries.
*
* <p>Such auto-proxying is particularly useful if there's a large number of beans that need
* to be wrapped with similar proxies, i.e. delegating to the same interceptors.
* Instead of x repetitive proxy definitions for x target beans, you can register
* one single such post processor with the bean factory to achieve the same effect.
*
* <p>Subclasses can apply any strategy to decide if a bean is to be proxied,
* e.g. by type, by name, by definition details, etc. They can also return
* additional interceptors that should just be applied to the specific bean
* instance. The default concrete implementation is BeanNameAutoProxyCreator,
* identifying the beans to be proxied via a list of bean names.
*
* <p>Any number of TargetSourceCreator implementations can be used with any subclass,
* to create a custom target source - for example, to pool prototype objects.
* Autoproxying will occur even if there is no advice if a TargetSourceCreator specifies
* a custom TargetSource. If there are no TargetSourceCreators set, or if none matches,
* a SingletonTargetSource will be used by default to wrap the bean to be autoproxied.
*
* @author Juergen Hoeller
* @author Rod Johnson
* @author Rob Harrop
* @since 13.10.2003
* @see #setInterceptorNames
* @see BeanNameAutoProxyCreator
*/
public abstract class AbstractAutoProxyCreator extends ProxyConfig
implements InstantiationAwareBeanPostProcessor, BeanFactoryAware, Ordered {
/**
* Convenience constant for subclasses: Return value for "do not proxy".
* @see #getAdvicesAndAdvisorsForBean
*/
protected static final Object[] DO_NOT_PROXY = null;
/**
* Convenience constant for subclasses: Return value for
* "proxy without additional interceptors, just the common ones".
* @see #getAdvicesAndAdvisorsForBean
*/
protected static final Object[] PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS = new Object[0];
/** Default value is same as non-ordered */
private int order = Integer.MAX_VALUE;
/** Default is global AdvisorAdapterRegistry */
private AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance();
/**
* Indicates whether or not the proxy should be frozen. Overridden from super to prevent the
* configuration from becoming frozen too early.
*/
private boolean freezeProxy;
/**
* Names of common interceptors. We must use bean name rather than object references
* to handle prototype advisors/interceptors.
* Default is the empty array: no common interceptors.
*/
private String[] interceptorNames = new String[0];
private boolean applyCommonInterceptorsFirst = true;
private TargetSourceCreator[] customTargetSourceCreators;
private BeanFactory beanFactory;
/**
* Set the ordering which will apply to this class's implementation
* of Ordered, used when applying multiple BeanPostProcessors.
* Default value is Integer.MAX_VALUE, meaning that it's non-ordered.
* @param order ordering value
*/
public final void setOrder(int order) {
this.order = order;
}
public final int getOrder() {
return order;
}
public boolean isFrozen() {
return this.freezeProxy;
}
/**
* Sets whether or not the proxy should be frozen, preventing advice from being added to it
* once it is created. Overridden from the super class to prevent the proxy configuration from
* being frozen before the proxy is created.
*/
public void setFrozen(boolean frozen) {
this.freezeProxy = frozen;
}
/**
* Specify the AdvisorAdapterRegistry to use.
* Default is the global AdvisorAdapterRegistry.
* @see org.springframework.aop.framework.adapter.GlobalAdvisorAdapterRegistry
*/
public void setAdvisorAdapterRegistry(AdvisorAdapterRegistry advisorAdapterRegistry) {
this.advisorAdapterRegistry = advisorAdapterRegistry;
}
/**
* Set custom TargetSourceCreators to be applied in this order.
* If the list is empty, or they all return null, a SingletonTargetSource
* will be created for each bean.
* <p>Note that TargetSourceCreators will kick in even for target beans
* where no advices or advisors have been found. If a TargetSourceCreator
* returns a TargetSource for a specific bean, that bean will be proxied
* in any case.
* <p>TargetSourceCreators can only be invoked if this post processor is used
* in a BeanFactory, and its BeanFactoryAware callback is used.
* @param targetSourceCreators list of TargetSourceCreator.
* Ordering is significant: The TargetSource returned from the first matching
* TargetSourceCreator (that is, the first that returns non-null) will be used.
*/
public void setCustomTargetSourceCreators(TargetSourceCreator[] targetSourceCreators) {
this.customTargetSourceCreators = targetSourceCreators;
}
/**
* Set the common interceptors. These must be bean names in the current factory.
* They can be of any advice or advisor type Spring supports. If this property
* isn't set, there will be zero common interceptors. This is perfectly valid,
* if "specific" interceptors such as matching Advisors are all we want.
*/
public void setInterceptorNames(String[] interceptorNames) {
this.interceptorNames = interceptorNames;
}
/**
* Set whether the common interceptors should be applied before bean-specific ones.
* Default is "true"; else, bean-specific interceptors will get applied first.
*/
public void setApplyCommonInterceptorsFirst(boolean applyCommonInterceptorsFirst) {
this.applyCommonInterceptorsFirst = applyCommonInterceptorsFirst;
}
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
/**
* Return the owning BeanFactory
* May be <code>null</code>, as this object doesn't need to belong to a bean factory.
*/
protected BeanFactory getBeanFactory() {
return this.beanFactory;
}
public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException {
if (isInfrastructureClass(beanClass, beanName)) {
return null;
}
// Create proxy here if we have a custom TargetSource.
// Suppresses unnecessary default instantiation of the target bean:
// The TargetSource will handle target instances in a custom fashion.
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
if (targetSource != null) {
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
return createProxy(beanClass, beanName, specificInterceptors, targetSource);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -