spring复习:(37)ProxyFactoryBean之getObject

该工厂bean的getObject代码如下:

	public Object getObject() throws BeansException {
		initializeAdvisorChain();
		if (isSingleton()) {
			return getSingletonInstance();
		}
		else {
			if (this.targetName == null) {
				logger.info("Using non-singleton proxies with singleton targets is often undesirable. " +
						"Enable prototype proxies by setting the 'targetName' property.");
			}
			return newPrototypeInstance();
		}
	}

该方法主要包含两部分内容:初始化advisor链,返回singleton instance.
其中调用的initializeAdvisorChain方法代码如下:

	private synchronized void initializeAdvisorChain() throws AopConfigException, BeansException {
		if (this.advisorChainInitialized) {
			return;
		}

		if (!ObjectUtils.isEmpty(this.interceptorNames)) {
			if (this.beanFactory == null) {
				throw new IllegalStateException("No BeanFactory available anymore (probably due to serialization) " +
						"- cannot resolve interceptor names " + Arrays.asList(this.interceptorNames));
			}

			// Globals can't be last unless we specified a targetSource using the property...
			if (this.interceptorNames[this.interceptorNames.length - 1].endsWith(GLOBAL_SUFFIX) &&
					this.targetName == null && this.targetSource == EMPTY_TARGET_SOURCE) {
				throw new AopConfigException("Target required after globals");
			}

			// Materialize interceptor chain from bean names.
			for (String name : this.interceptorNames) {
				if (name.endsWith(GLOBAL_SUFFIX)) {
					if (!(this.beanFactory instanceof ListableBeanFactory)) {
						throw new AopConfigException(
								"Can only use global advisors or interceptors with a ListableBeanFactory");
					}
					addGlobalAdvisors((ListableBeanFactory) this.beanFactory,
							name.substring(0, name.length() - GLOBAL_SUFFIX.length()));
				}

				else {
					// If we get here, we need to add a named interceptor.
					// We must check if it's a singleton or prototype.
					Object advice;
					if (this.singleton || this.beanFactory.isSingleton(name)) {
						// Add the real Advisor/Advice to the chain.
						advice = this.beanFactory.getBean(name);
					}
					else {
						// It's a prototype Advice or Advisor: replace with a prototype.
						// Avoid unnecessary creation of prototype bean just for advisor chain initialization.
						advice = new PrototypePlaceholderAdvisor(name);
					}
					addAdvisorOnChainCreation(advice);
				}
			}
		}

		this.advisorChainInitialized = true;
	}

主要是遍历给ProxyFactoryBean配置的interceptorNames,对每个name,从bean factory中获取bean,然后调用addAdvisorOnCreation将其添加到advisor chain中:
spring复习:(37)ProxyFactoryBean之getObject_第1张图片
然后调用getSingletonInstance方法,该方法代码如下:
spring复习:(37)ProxyFactoryBean之getObject_第2张图片
其中this.singletonInstance = getProxy(createAopProxy());就创建了代理对象。
其中createAopProxy的代码如下(位于DefaultAopProxyFactory类):
spring复习:(37)ProxyFactoryBean之getObject_第3张图片
它会返回一个ObjenesisAopProxy或者JdkDynamicAopProxy对象,然后把它传递给getProxy方法,而getProxy方法代码如下:
spring复习:(37)ProxyFactoryBean之getObject_第4张图片
该getProxy调用的getProxy方法在JdkDynamicAopProxy中的实现如下:
spring复习:(37)ProxyFactoryBean之getObject_第5张图片
可见其使用了Jdk 动态代理生成了一个Proxy的子类的对象然后返回。

你可能感兴趣的:(Spring,spring,原型模式,java)