SpringでBenFactoryを取る

5124 ワード

Springによると、Bean FactoryAwareを実現すると、このbeanがいるコンテキストのBenFactoryを取得することができるという。
<?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>
上のbaserviceImplはBenFactoryAwareインターフェースを実現しました。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をプリントアウトしました。
arg 0=org.springframe ewark.beans.factory.support.DefaultListableBeanFactory@a9255c:defining beans[codehelpcommand、baseServiceImpl]root of factory hierarchy
 
2箇所のメソッドを呼び出した場合、エラーが発生しました。beanFactoryは空です。
 
GTWと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;		
	}
}
 
このコンテントxtUtilを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で取得し、この類は自分でもうBenFactoryAwareを実現しなくてもいいです。
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はデフォルトです。悩みます。穴を掘って、二日間で埋めます。