如何取得Spring管理的bean

本文主要讲3中实现方式,请用第3种方法(通用)

1、servlet方式加载时配置如下

<servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> 

spring容器放在ServletContext中的key是org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC

注意后面的springMVC,是你的servlet-name配置的值,注意适时修改。

ServletContext sc=略 WebApplicationContext attr = (WebApplicationContext)sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC"); 

2、listener方式加载

web.xml如下:

<context-param>  <param-name>contextConfigLocation</param-name>  <param-value>/WEB-INF/applicationContext</param-value> </context-param> 

<listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 

【jsp/servlet】可以这样取得

ServletContext context = getServletContext(); WebApplicationContext applicationContext = WebApplicationContextUtils .getWebApplicationContext(context);

3、通用的方法来了,前的 1、2两种方法并不通用,可以抛弃了。

在配置文件中加入:

<!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean(‘xxxx‘)的静态方法得到spring bean对象 --> <bean class="com.xxxxx.SpringContextHolder" lazy-init="false" /> 

获取如下:

import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /**  * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext.  *  */ public class SpringContextHolder implements ApplicationContextAware { private static ApplicationContext applicationContext; 

/** * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量. */ public void setApplicationContext(ApplicationContext applicationContext) { SpringContextHolder.applicationContext = applicationContext; // NOSONAR } 

/** * 取得存储在静态变量中的ApplicationContext. */ public static ApplicationContext getApplicationContext() { checkApplicationContext(); return applicationContext; } 

/** * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) { checkApplicationContext(); return (T) applicationContext.getBean(name); } 

/** * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. */ @SuppressWarnings("unchecked") public static <T> T getBean(Class<T> clazz) { checkApplicationContext(); return (T) applicationContext.getBeansOfType(clazz); } 

/** * 清除applicationContext静态变量. */ public static void cleanApplicationContext() { applicationContext = null; } 

private static void checkApplicationContext() { if (applicationContext == null) { throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder"); } } } 

原文地址:https://www.cnblogs.com/mengtaoadmin/p/11184002.html

时间: 2024-08-01 05:57:05

如何取得Spring管理的bean的相关文章

Spring管理的Bean的生命周期

bean的初始化时机 前面讲解了Spring容器管理的bean的作用域.接着我们就要思考一个问题:bean到底是在什么时候才进行实例化的呢?我们以这个问题为引子来展开本文的说明. bean对象无外乎是在以下两个时刻进行实例化的: 调用getBean()方法时. Spring容器启动时. 那么bean对象到底是在哪个时刻进行实例化的,这与Bean的作用域有着某种联系.我们以配置Spring管理的bean的作用域的案例为基础进行深入探讨.为了能够清楚地看到bean对象的实例化,我们需要修改Perso

将spring管理的bean使用注解的方式注入到servlet中

Filter和Servlet中不能直接注解使用spring的bean,因为这两个都是servlet容器维护管理的,当然也有实现方法,如下: 1.创建一个AbstractServlet 抽象类,让你的所有servlet继承于此类: import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpSe

在普通类中获取Spring管理的bean

1.在项目中添加下面的类: import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. * */ public class SpringContextHolder implem

转:如何取得Spring管理的bean

原文链接:http://blog.csdn.net/a9529lty/article/details/42145545 1.servlet方式加载时,[web.xml] Xml代码 <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <

配置spring管理的bean的作用域

.singleton 在每一个spring Ioc容器中一个bean定义只有一个对象实例.默认情况下会在容器启动时初始化bean,但我们可以指定bean节点的lazy-init = "true"来延迟初始化bean,这时候,只有第一次获取bean才会初始化bean.如下: 1 <bean id = "xxx" class = "cn.itcast.OrderServiceBean" lazy-init = "ture"/

JSP获取Spring管理的bean

<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%><%@ page import="org.springframework.context.ApplicationContext"%> ServletContext sc = this.getServletConfig().getServletContext(); Appli

spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常

java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常 这个错误,网上说的原因一大推,反正我这个出的问题 是因为 虽然我在 spring 里把事务都配置好了,结果运行就出现这个错误,看看配置都没什么问题,都是网上 案例 照 写编码的,还是错的,结果发现是因为 我

Spring Boot集成Quartz注入Spring管理的类

摘要: 在Spring Boot中使用Quartz时,在JOB中一般需要引用Spring管理的Bean,通过定义Job Factory实现自动注入. Spring有自己的Schedule定时任务,在Spring boot中使用的时候,不能动态管理JOB,于是就使用Quartz来实现. 在Spring Boot中配置Quartz: import java.io.IOException; import java.util.Properties; import org.springframework.

spring中bean的五种作用域?Spring中的bean是线程安全的吗?

spring中bean的五种作用域 当通过spring容器创建一个Bean实例时,不仅可以完成Bean实例的实例化,还可以为Bean指定特定的作用域.Spring支持如下5种作用域: singleton:单例模式,在整个Spring IoC容器中,使用singleton定义的Bean将只有一个实例 prototype:原型模式,每次通过容器的getBean方法获取prototype定义的Bean时,都将产生一个新的Bean实例 request:对于每次HTTP请求,使用request定义的Bea