用BeanFactoryAware接口,用BEAN的名称来获取BEAN对象

用BeanFactoryAware接口,用BEAN的名称来获取BEAN对象


applicationContext-beans.xml
----------------------------
<bean id="beanFactoryHelper" class="com.cyjch.base.BeanFactoryHelper"/>

BeanFactoryHelper.java
----------------------------
package com.cyjch.base;

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

public class BeanFactoryHelper implements BeanFactoryAware {
	private static BeanFactory beanFactory; //BEAN工厂

	@Override
	public void setBeanFactory(BeanFactory f) throws BeansException {
		this.beanFactory = f; 
	}
	public static BeanFactory getBeanfactory() {   
		return beanFactory;   
	}

}


其它的实现类(可以继承BeanFactoryHelper并实现多个接口)
-----------------
public class PeixunServiceImpl extends BeanFactoryHelper implements IPeixunService,Serializable{
	BeanFactory bf = BeanFactoryHelper.getBeanfactory();//获取工厂
	...
	IPeixunDao peixunDAO = (PeixunDaoImpl) bf.getBean("peixunDao");//通过BEAN名称取BEAN
	IUserDao userdao = (UserDaoImpl) bf.getBean("userDao");

	XxxService xxxService;
	getter/setter;
	XxxDao xxxDao;
	getter/setter;
}

  

posted @ 2012-02-06 17:16  cyjch  阅读(5536)  评论(0编辑  收藏  举报