论坛首页 Java企业应用论坛

关于spring ioc容器的问题

浏览 7195 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-06-29  
在spring的源代码中,有org.springframework.beans.factory.support.BeanDefinitionValueResolver这么个类.
在该类中有private Object resolveReference(String argName, RuntimeBeanReference ref)这样1个方法.

源代码如下:

private Object resolveReference(String argName, RuntimeBeanReference ref) {
		if (this.beanFactory.logger.isTraceEnabled()) {
			this.beanFactory.logger.trace("Resolving reference from property " + argName + " in bean '" +
					this.beanName + "' to bean '" + ref.getBeanName() + "'");
		}
		try {
			if (ref.isToParent()) {
				if (this.beanFactory.getParentBeanFactory() == null) {
					throw new BeanCreationException(
							this.beanDefinition.getResourceDescription(), this.beanName,
							"Can't resolve reference to bean '" + ref.getBeanName() +
							"' in parent factory: no parent factory available");
				}
				return this.beanFactory.getParentBeanFactory().getBean(ref.getBeanName());
			}
			else {
				Object bean = this.beanFactory.getBean(ref.getBeanName());
				if (this.beanDefinition.isSingleton()) {
					this.beanFactory.registerDependentBean(ref.getBeanName(), this.beanName);
				}
				return bean;
			}
		}
		catch (BeansException ex) {
			throw new BeanCreationException(
					this.beanDefinition.getResourceDescription(), this.beanName,
					"Cannot resolve reference to bean '" + ref.getBeanName() + "' while setting " + argName, ex);
		}
	}


这个方法的大致作用是,取得bean所依赖的bean实例.但这里遇到问题了.ioc容器在这里做了1个依赖关系的map.但建立了这个依赖关系后,也没有具体使用到他的地方.难以理解设计者的思想.

请各位帮忙解答.谢谢
   发表时间:2007-06-29  
我想,应该指的是这段代码:

			if (this.beanDefinition.isSingleton()) {
					this.beanFactory.registerDependentBean(ref.getBeanName(), this.beanName);
				}
				return bean;
			}

这里面注册的这个map起的是什么作用?好像没有看到什么地方用到它。
0 请登录后投票
   发表时间:2007-07-03  
我想他是用在清理内存的时候吧。你可以看abstractBeanFactroy里的代码片段
0 请登录后投票
   发表时间:2007-07-03  
baallee 写道
我想他是用在清理内存的时候吧。你可以看abstractBeanFactroy里的代码片段

能不能说得详细一些?大家一起讨论讨论
0 请登录后投票
   发表时间:2007-07-03  
/**
	 * Add the given bean to the list of disposable beans in this factory,
	 * registering its DisposableBean interface and/or the given destroy method
	 * to be called on factory shutdown (if applicable). Only applies to singletons.
	 * <p>Also registers bean as dependent on other beans, according to the
	 * "depends-on" configuration in the bean definition.
	 * @param beanName the name of the bean
	 * @param bean the bean instance
	 * @param mbd the bean definition for the bean
	 * @see RootBeanDefinition#isSingleton
	 * @see RootBeanDefinition#getDependsOn
	 * @see #registerDisposableBean
	 * @see #registerDependentBean
	 */
	protected void registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mbd) {
		if (mbd.isSingleton() && requiresDestruction(bean, mbd)) {
			// Register a DisposableBean implementation that performs all destruction
			// work for the given bean: DestructionAwareBeanPostProcessors,
			// DisposableBean interface, custom destroy method.
			registerDisposableBean(beanName,
					new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors()));

			// Register bean as dependent on other beans, if necessary,
			// for correct shutdown order.
			String[] dependsOn = mbd.getDependsOn();
			if (dependsOn != null) {
				for (int i = 0; i < dependsOn.length; i++) {
					registerDependentBean(dependsOn[i], beanName);
				}
			}
		}
	}


comment中有提到当destroy method to be called on factory shutdown,只是猜想没深入看源码
0 请登录后投票
   发表时间:2007-07-03  
baallee 写道
	protected void registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mbd) {
		if (mbd.isSingleton() && requiresDestruction(bean, mbd)) {
			// Register a DisposableBean implementation that performs all destruction
			// work for the given bean: DestructionAwareBeanPostProcessors,
			// DisposableBean interface, custom destroy method.
			registerDisposableBean(beanName,
					new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors()));

			// Register bean as dependent on other beans, if necessary,
			// for correct shutdown order.
			String[] dependsOn = mbd.getDependsOn();
			if (dependsOn != null) {
				for (int i = 0; i < dependsOn.length; i++) {
					registerDependentBean(dependsOn[i], beanName);
				}
			}
		}
	}


comment中有提到当destroy method to be called on factory shutdown,只是猜想没深入看源码

奇怪的是我在ResolveReference也就是依赖注入的时候看到这个registerDependentBean,同时也在
createBean的最后一部registerDisposableBeanIfNecessary(beanName, originalBean, mergedBeanDefinition)中调用了,为什么要调用两次呢?
调用的代码是:
	public void registerDependentBean(String beanName, String dependentBeanName) {
		synchronized (this.dependentBeanMap) {
			Set dependencies = (Set) this.dependentBeanMap.get(beanName);
			if (dependencies == null) {
				dependencies = CollectionFactory.createLinkedSetIfPossible(8);
				this.dependentBeanMap.put(beanName, dependencies);
			}
			dependencies.add(dependentBeanName);
		}
	}

当然在registerDisposableBeanIfNecessary里面调用,可以保证能在shutdown的时候根据适当的依赖顺序来dispose,但是弄不清楚的是在ResolveReference里,记录这些依赖信息什么时候会被使用?
0 请登录后投票
   发表时间:2007-10-16  
在Spring源码的注释上写的很清楚,在单态bean销毁的时候,会按照这个map中定义的顺序进行。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics