zoukankan      html  css  js  c++  java
  • 用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;
    }
    

      

  • 相关阅读:
    构造并判断二叉搜索树-js
    构造树并判断是否对称
    旋转图像
    螺旋矩阵
    链表实现快速排序
    任务调度器
    队列的实现
    最大矩形
    棒球比赛
    复原IP地址
  • 原文地址:https://www.cnblogs.com/cyjch/p/2340417.html
Copyright © 2011-2022 走看看