`

Spring中取BeanFactory

 
阅读更多

      Spring中说了,一个bean实现BeanFactoryAware,就可以取得这个bean所在的上下文的BeanFactory:

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean name="codehelpcommand" class="com.zyp.finance.server.CodeHelpCommand"/>
<bean name="baseServiceImpl" class="com.zyp.finance.server.BaseServiceImpl"/>
 </beans>

上面的baseServiceImpl实现了BeanFactoryAware接口,按理应该可以获取到codehelpcommand这个bean的,结果下面的小测试出了问题:

 

package com.zyp.finance.server;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.zyp.finance.client.BaseService;
import com.zyp.finance.client.RequestData;

/**
 * The server side implementation of the RPC service.
 */
public class BaseServiceImpl extends RemoteServiceServlet implements
	BaseService, BeanFactoryAware{
	private BeanFactory beanFactory;
	public RequestData baseServer(RequestData data) {
		String action = data.getActionName();
		ICommand command = fetchCommand(action);
		RequestData response = command.execute(data);
		return response;
	}
	


	public void setBeanFactory(BeanFactory arg0) throws BeansException {
		System.out.println("arg0="+arg0);//1:打印出了2个bean
		beanFactory = arg0;
		
	}
	
	//通过spring获取对应的bean处理
	public ICommand fetchCommand(String action){
		System.out.println("action="+action);
		if(this.beanFactory==null){
			System.out.println("this.beanFactory is null");//2:打印出了这句,beanFactory为空。
		}
		Object bean =  this.beanFactory.getBean(action);
		if(bean==null){
			System.out.println("error,no such bean:"+action);
			return null;
		}
		else return (ICommand) bean;
		
	}

}
 

启动服务器的时候,打印出了1处的context中的其他bean:

arg0=org.springframework.beans.factory.support.DefaultListableBeanFactory@a9255c: defining beans [codehelpcommand,baseServiceImpl]; root of factory hierarchy

 

调用2处的方法的时候,报错,beanFactory为空。

 

当初整合GWT和Spring的时,想起同事写了个单例来存放beanFactory,按照那个思路改了下代码,就好了:

 

package com.zyp.finance.server;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

public class ContextUtil implements BeanFactoryAware{
	private static  BeanFactory beanFactory;
	private static  ContextUtil instance = new ContextUtil();
	public  BeanFactory getBeanFactory() {
		return beanFactory;
	}

	private ContextUtil(){
		
	}
	
	public static ContextUtil getInstance(){
		return instance;
	}

	public void setBeanFactory(BeanFactory arg0) throws BeansException {
		beanFactory = arg0;		
	}
}

 

将这个ContextUtil放在xml中去加载:

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean name="codehelpcommand" class="com.zyp.finance.server.CodeHelpCommand"/>
<bean name="baseServiceImpl" class="com.zyp.finance.server.BaseServiceImpl"/>
<bean name="contextUtil" class="com.zyp.finance.server.ContextUtil"/>
 </beans>

 BaseServiceImpl获取,并且这个类自己不用再实现BeanFactoryAware了。

package com.zyp.finance.server;

import org.springframework.beans.factory.BeanFactory;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.zyp.finance.client.BaseService;
import com.zyp.finance.client.RequestData;

public class BaseServiceImpl extends RemoteServiceServlet implements
	BaseService{
	private BeanFactory beanFactory = ContextUtil.getInstance().getBeanFactory();
	
	//通过spring获取对应的bean处理
	public ICommand fetchCommand(String action){
		Object bean =  this.beanFactory.getBean(action);
	         //成功获取到了,其他代码省略。
		
	}

}

 至于为什么会这样。。不清楚了。。明明bean都是默认单例的。。小纠结~挖个坑,二天来埋

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics