Spring 4.3ソース分析の配置式Aop(一)
5338 ワード
1.Spring Aop配置式Aop Demo
上編では、手動でコードを書き、Pointcut、MethodInterceptor、ProxyFactory、Target Benを組み立て、最終的にProxyFactory.get Proxy()を通じて代理の対象を獲得しました.本編ではSpringのFactoryBenを結合し、xmlに少しの情報を配置するだけでターゲットオブジェクトに対してプロキシオブジェクトを生成することができます.構成情報は以下の通りです.
2.Spring Aop配置式AopコンポーネントProxyFactoryBen
ProxyFactoryBeanはFactoryBeanを通じて指定されたtargaBenに対して動的代理オブジェクトを作成します.(PS:FactoryBeanはSpring IOCで指定対象の抽象的な工場を作るために、いくつかの種類の作成過程は複雑です.だからFactoryBenというキャラクターが現れました.).
プロキシオブジェクト全体を作成するプロセスは、ProxyFactoryBeanのgetObject方法でトリガされるものであり、大まかな流れは以下の通りである.
この物語はProxyFactoryBeanを通じて代理オブジェクトを作成します.全体の作成過程は前編と同じです.つまりProxyFactoryBeanはMethodInterceptorの自動取得だけでなく、ProxyFactoryを通じて代理オブジェクトを作成します.問題はProxyFactoryBenの中にあります.この種類から、ダイナミックエージェントを作成する必要があります.ProxyFactoryBeanを配置する必要があります.
5.参考:
Spring Aopの核心的なソースコード分析Spring技術の内幕Springのオープンソースの深さを分析するために、Springのソースコード分析Springのソースコードの傷神Springのソースコードの分析
上編では、手動でコードを書き、Pointcut、MethodInterceptor、ProxyFactory、Target Benを組み立て、最終的にProxyFactory.get Proxy()を通じて代理の対象を獲得しました.本編ではSpringのFactoryBenを結合し、xmlに少しの情報を配置するだけでターゲットオブジェクトに対してプロキシオブジェクトを生成することができます.構成情報は以下の通りです.
com.lami.foodie.aop.aop1.KK
simpleMethodInterceptor
PS:上のプロファイルに対応するクラスは前のページに既に存在します.2.Spring Aop配置式AopコンポーネントProxyFactoryBen
ProxyFactoryBeanはFactoryBeanを通じて指定されたtargaBenに対して動的代理オブジェクトを作成します.(PS:FactoryBeanはSpring IOCで指定対象の抽象的な工場を作るために、いくつかの種類の作成過程は複雑です.だからFactoryBenというキャラクターが現れました.).
/**
* This suffix in a value in an interceptor list indicates to expand globals.
*/
public static final String GLOBAL_SUFFIX = "*";
protected final Log logger = LogFactory.getLog(getClass());
// , , , advice/MethodInterceptor, target (PS: checkInterceptorNames )
private String[] interceptorNames;
// target, String xml , checkInterceptorNames
private String targetName;
// target interface
private boolean autodetectInterfaces = true;
// PS: singleton advice singleton
private boolean singleton = true;
// Spring aop MethodInterceptor, xml advice, -> advice MethodInterceptor
// advisorAdapterRegistry advice, MethodInterceptor Advisor, Advisor MethodInterceptor
private AdvisorAdapterRegistry advisorAdapterRegistry = GlobalAdvisorAdapterRegistry.getInstance();
//
private boolean freezeProxy = false;
// classLoader
private transient ClassLoader proxyClassLoader = ClassUtils.getDefaultClassLoader();
// classLoader
private transient boolean classLoaderConfigured = false;
// BeanFactory
private transient BeanFactory beanFactory;
// advisorChain
/** Whether the advisor chain has already been initialized */
private boolean advisorChainInitialized = false;
//
/** If this is a singleton, the cached singleton proxy instance */
private Object singletonInstance;
これらのプロパティの中で最も重要なのは、インターフェースNames、ターゲットName(代理のクラス)であり、advice/advisorをMethodInterceptorのアダプターに変換して工場advisor AdapterRegistryに登録します.以上の属性があれば、次の2つの方法で代理人を作成できます.public Object getObject() throws BeansException {
//
initializeAdvisorChain();
// Singleton prototype , Proxy
if (isSingleton()) {
return getSingletonInstance();
}
else { // prototype
if (this.targetName == null) {
logger.warn("Using non-singleton proxies with singleton targets is often undesirable. " +
"Enable prototype proxies by setting the 'targetName' property.");
}
return newPrototypeInstance();
}
}
// singletonInstance
private synchronized Object getSingletonInstance() {
if (this.singletonInstance == null) {
this.targetSource = freshTargetSource(); // target
//
if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) {
// Rely on AOP infrastructure to tell us what interfaces to proxy.
Class> targetClass = getTargetClass();
if (targetClass == null) {
throw new FactoryBeanNotInitializedException("Cannot determine target class for proxy");
}
//
setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader)); // targetClass interface
}
// Initialize the shared singleton instance.
super.setFrozen(this.freezeProxy);
// ProxyFactory
// createAopProxy AopProxy(JdkDynamicAopProxy, CglibAopProxy), AopProxy getProxy
this.singletonInstance = getProxy(createAopProxy());
}
return this.singletonInstance;
}
3.Spring Aop配置式Aop ProxyFactoryBean作成対象プロセスプロキシオブジェクト全体を作成するプロセスは、ProxyFactoryBeanのgetObject方法でトリガされるものであり、大まかな流れは以下の通りである.
1. AdvisorChain
1.1 interceptorNames, BeanFactory Advice, MethodInterceptor, AdvisorAdaptorRegistry Advisor AdvisorChain
2. getSingletonInstance ,
2.1 targetName BeanFactory
2.2 target
4.まとめ:この物語はProxyFactoryBeanを通じて代理オブジェクトを作成します.全体の作成過程は前編と同じです.つまりProxyFactoryBeanはMethodInterceptorの自動取得だけでなく、ProxyFactoryを通じて代理オブジェクトを作成します.問題はProxyFactoryBenの中にあります.この種類から、ダイナミックエージェントを作成する必要があります.ProxyFactoryBeanを配置する必要があります.
5.参考:
Spring Aopの核心的なソースコード分析Spring技術の内幕Springのオープンソースの深さを分析するために、Springのソースコード分析Springのソースコードの傷神Springのソースコードの分析