SpringBeanFactoryUtils  源代码:

package com.common.common.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringBeanFactoryUtils   implements ApplicationContextAware {    
    private static ApplicationContext appCtx;    
    /**  
     * 此方法可以把ApplicationContext对象inject到当前类中作为一个静态成员变量。  
     * @param applicationContext ApplicationContext 对象.  
     * @throws BeansException  
     * @author wangdf 
     */    
     
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {    
        appCtx = applicationContext;    
    }  
       
    /** 
     * 获取ApplicationContext 
     * @return 
     * @author wangdf 
     */  
    public static ApplicationContext getApplicationContext(){  
        return appCtx;  
    }  
       
    /**  
     * 这是一个便利的方法,帮助我们快速得到一个BEAN  
     * @param beanName bean的名字  
     * @return 返回一个bean对象  
     * @author wangdf 
     */    
    public static Object getBean(String beanName) {    
        return appCtx.getBean(beanName);    
    }    
}   

spring 配置文件:


实例代码:

private IAlarmInstallPositionService alarmInstallPositionService =(IAlarmInstallPositionService)SpringBeanFactoryUtils.getBean("alarmInstallPositionService");
	
	private IHomeDeviceService deviceService =(IHomeDeviceService)SpringBeanFactoryUtils.getBean("homeDeviceService");;

	private IHomeDeviceCommandService deviceCommandService  = (IHomeDeviceCommandService)SpringBeanFactoryUtils.getBean("homeDeviceCommandService");;
	
	private IHomeDeviceAttrStatuService deviceAttrStatuService = (IHomeDeviceAttrStatuService)SpringBeanFactoryUtils.getBean("homeDeviceAttrStatuService");
	
	private IHomeDeviceLogService deviceLogService  = (IHomeDeviceLogService)SpringBeanFactoryUtils.getBean("homeDeviceLogService");;
	
	private IHomeDeviceAlarmService deviceAlarmService  = (IHomeDeviceAlarmService)SpringBeanFactoryUtils.getBean("homeDeviceAlarmService");;

	private IHomeGatewayService gatewayService = (IHomeGatewayService)SpringBeanFactoryUtils.getBean("homeGatewayService");

	public IAlarmInstallPositionService getAlarmInstallPositionService() {
		return alarmInstallPositionService;
	}

	public void setAlarmInstallPositionService(
			IAlarmInstallPositionService alarmInstallPositionService) {
		this.alarmInstallPositionService = alarmInstallPositionService;
	}

	public IHomeDeviceService getDeviceService() {
		return deviceService;
	}

	public void setDeviceService(IHomeDeviceService deviceService) {
		this.deviceService = deviceService;
	}

	public IHomeDeviceCommandService getDeviceCommandService() {
		return deviceCommandService;
	}

	public void setDeviceCommandService(
			IHomeDeviceCommandService deviceCommandService) {
		this.deviceCommandService = deviceCommandService;
	}

	public IHomeDeviceAttrStatuService getDeviceAttrStatuService() {
		return deviceAttrStatuService;
	}

	public void setDeviceAttrStatuService(
			IHomeDeviceAttrStatuService deviceAttrStatuService) {
		this.deviceAttrStatuService = deviceAttrStatuService;
	}

	public IHomeDeviceLogService getDeviceLogService() {
		return deviceLogService;
	}

	public void setDeviceLogService(IHomeDeviceLogService deviceLogService) {
		this.deviceLogService = deviceLogService;
	}

	public IHomeDeviceAlarmService getDeviceAlarmService() {
		return deviceAlarmService;
	}

	public void setDeviceAlarmService(IHomeDeviceAlarmService deviceAlarmService) {
		this.deviceAlarmService = deviceAlarmService;
	}

	public IHomeGatewayService getGatewayService() {
		return gatewayService;
	}

	public void setGatewayService(IHomeGatewayService gatewayService) {
		this.gatewayService = gatewayService;
	}


Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐