`
xurichusheng
  • 浏览: 336710 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Spring工具类,提供取得Spring配置文件中定义的Bean的方法

 
阅读更多

 

       在 java EE 工程中,经常会用到定时任务Job、自定义servlet,在这些job、servlet中,也会用到在 spring 容器中声明的bean,如 service、dao等。

       在 job、servlet 中,这些 bean 的获取如下所示:

 

1. 自定义spring加载器

          这个加载器的作用就是把 ApplicationContext 装载到 SpringUtil 中。

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * @ClassName: SpringContextLoaderListener
 * @deprecated: 自定义spring加载器,把ApplicationContext装载到SpringUtil
 * @author 
 * @company 
 * @date 2014-5-15
 * @version V1.0
 */

public class SpringContextLoaderListener extends ContextLoaderListener {

	public void contextInitialized(ServletContextEvent event) {

		// 初始化父类 ContextLoaderListener
		super.contextInitialized(event);

		ServletContext context = event.getServletContext();

		ApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext(context);

		// 装载 SpringUtil 中的 ApplicationContext
		new SpringUtil().setApplicationContext(ctx);
	}
}

 

2. 将 SpringContextLoaderListener 加入 web.xml

 

注释 org.springframework.web.context.ContextLoaderListener

 

添加 SpringContextLoaderListener 



 

3. Spring工具类

   这个类的作用就是获取在 spring 容器中声明的 bean

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

/**
 * @ClassName: SpringUtil
 * @deprecated: Spring工具类,提供取得Spring配置文件中定义的Bean的方法
 * @author 
 * @company 
 * @date 2014-5-15
 * @version V1.0
 */

public class SpringUtil implements ApplicationContextAware {

	private static ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {

		// 在 SpringContextLoaderListener 中调用此方法

		SpringUtil.applicationContext = arg0;
	}

	public static Object getBean(String name) {
		
		// applicationContext 已在 SpringContextLoaderListener 中装载

		return applicationContext.getBean(name);
	}
}

 

4. 在 servlet 或者 job 中获取 spring 容器中声明的 bean

private HwSysParamsService hwSysParamsService;

hwSysParamsService = (HwSysParamsService) SpringUtil
					.getBean("hwSysParamsService");

 

  • 大小: 31.3 KB
分享到:
评论

相关推荐

    创建SpringBean配置工具类

    创建SpringBean配置工具类(安全)如: <bean id=... scope="prototype"></bean>

    Spring boot工具类静态属性注入及多环境配置详解

    主要为大家详细介绍了Spring boot工具类静态属性注入,及多环境配置详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    spring-struts1-strust2-hibernate 核心包介绍

    这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个jar文件是所有应用...

    spring jar 包详解

    (1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个...

    Spring cloud 微服务框架 Mica 中的基础组件,用来生成 Spring boot starter 的一些基础配置

    mica-auto 是 Spring cloud 微服务框架 Mica 中的一个基础组件,用来生成 Spring boot starter 的一些基础配置。

    Spring实例化Bean顺序

    NULL 博文链接:https://88548886.iteye.com/blog/1682243

    spring-aop-5.1.0.RELEASE.jar

    spring-**core**-4.3.6.RELEASE.jar :包含spring框架基本的核心工具类,spring其他组件都要用到这个包里的类,其他组件的基本核心 spring-**beans**-4.3.6.RELEASE.jar:所有应用都要用到的jar包,它包含访问配置...

    spring框架约束步骤及教程

    spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其他组件都要使用到这个包里的类,是其他组件的基本核心,当然你也能在自己的应用系统中使用这些工具类。 spring-beans.jar 这个jar文件是所有...

    Spring中文帮助文档

    3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 3.11.1. @Autowired 3.11.2. 基于注解的自动连接微调 3.11.3. CustomAutowireConfigurer 3.11.4. @...

    Spring-Reference_zh_CN(Spring中文参考手册)

    12.2.2. 在Spring的application context中创建 SessionFactory 12.2.3. HibernateTemplate 12.2.4. 不使用回调的基于Spring的DAO实现 12.2.5. 基于Hibernate3的原生API实现DAO 12.2.6. 编程式的事务划分 12.2.7. ...

    基于Spring MVC的web框架 1.1.11

    ftp服务端和客户端工具类,配置文件maven和web项目路径统一 1.1.0 soapui工具类(web版本) properties等工具类 1.1.1 工具类数据校验 jsp自定义标签 Spring自定义注解 默认requestMapping 1.1.2 代码生成器 1.1.3 ...

    spring配置bean注入为空的问题,求帮助

    NULL 博文链接:https://pczhuce-126-com.iteye.com/blog/1315170

    Util通用工具类(轮子类)

    避免重复造轮子,开发中常用封装...渲染工具类,资源文件相关的操作类,对比两个对象的变化的工具类,Spring的ApplicationContext的持有者,可以用静态方法的方式获取spring容器中的bean,sql语句工具类,高频方法集合类

    spring 中特殊bean用法

    NULL 博文链接:https://brighter.iteye.com/blog/1933260

    spring4.3.9相关jar包

    spring-core.jar(必须):这个jar 文件包含Spring 框架基本的核心工具类。Spring 其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 外部依赖Commons ...

    最新最全的spring开发包

    这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个jar文件是所有应用都要...

    Spring核心注解深入解析:提升开发效率

    @Configuration 和 @Bean 注解用于Java配置,允许开发者用程序的方式定义Spring容器中的bean,取代传统的XML配置文件。 @Value 提供了一种方便的方式来注入简单类型的属性,支持SpEL表达式和外部配置。

    spring-framework-5.1.2.RELEASE.rar

    spring-core-5.1.8.RELEASE.jar //Spring的核心工具类,其它jar包是建立这个包基础上的,都要用到这个包中的类。 spring-beans-5.1.8.RELEASE.jar //配置、创建、管理Bean,负责Ioc、DI spring-context-5.1.8....

    Spring 2.0 开发参考手册

    18.3. 使用Spring提供的辅助类实现EJB组件 19. JMS 19.1. 简介 19.2. 使用Spring JMS 19.2.1. JmsTemplate 19.2.2. 连接工厂 19.2.3. (消息)目的地管理 19.2.4. 消息侦听容器 19.2.5. 事务管理 19.3. 发送...

    普通类调用Spring bean对象

    NULL 博文链接:https://412887952-qq-com.iteye.com/blog/1479445

Global site tag (gtag.js) - Google Analytics