spring 通过中间层工具类SpringBeanFactoryUtils 实现service 注入
2016-10-26 17:24
585 查看
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;
}
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;
}
相关文章推荐
- Spring中通过实现FactoryBean接口获取bean实例
- Spring关于使用注解@Configuration去配置FormattingConversionServiceFactoryBean来实现自定义格式字符串处理无效的问题(未找到是什么原因造成的)
- Spring IOC/BeanFactory/ApplicationContext的工作流程/实现原理/初始化/依赖注入源码详解
- 原始方式:spring四种依赖注入方式 |注解方式:Spring零配置通过注解实现Bean依赖注入
- Spring中ApplicationContextAware实现获取bean的工具类,在servlet中通过servletContext获取application
- spring不通过注入的方式获取对象(BeanFactoryAware \ApplicationContextAware )
- spring 通过注解实现工具类injection Service方法
- Spring零配置通过注解实现Bean依赖注入总结<转>
- Spring定时任务的实现方式--ScheduledExecutorService and ScheduledExecutorFactoryBean的简单源码解析以及使用
- Spring通过SchedulerFactoryBean实现调度任务的配置
- Spring通过SchedulerFactoryBean实现调度任务的配置
- Spring通过SchedulerFactoryBean实现调度任务的配置
- 通过字符创调用接口中实现类的方法,SpringBean自动注入,
- 【第十二章】零配置 之 12.1 概述 12.2 注解实现Bean依赖注入 ——跟我学spring3
- spring工程中,不通过注入的方式获取bean
- 从源代码解读spring之DataSource实现和FactoryBean模式(JndiObjectFactoryBean)
- ssh,spring 泛型注入,泛型dao和service的实现
- Spring Web Service Client via JaxWsPortProxyFactoryBean
- spring 通过配置向quartz 注入service
- 通过Spring实现对自定义注解属性进行资源注入