spring 通过中间层工具类SpringBeanFactoryUtils 实现service 注入解析

熊孩纸 阅读:202 2021-03-31 21:42:27 评论:0

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; 
	}


声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们