【springMVC基础】spring获取bean的几种方法
2018-06-26 23:40
483 查看
1、ApplicationContext对象
说明:Spring框架程序通过配置文件手工初始化Spring的情况。
2、通过Spring提供的工具类获取ApplicationContext对象
说明:通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。
3、实现接口ApplicationContextAware
说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。Spring初始化时,会通过该方法将ApplicationContext对象注入。
package com.liu.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
}
4、通过Spring提供的ContextLoader
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
wac.getBean(beanID);
说明:注意一点,在服务器启动时,Spring容器初始化时,不能通过以上方法获取Spring 容器。
5、继承自抽象类ApplicationObjectSupport
说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取ApplicationContext。
Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。
6、继承自抽象类WebApplicationObjectSupport
说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId");
说明:Spring框架程序通过配置文件手工初始化Spring的情况。
2、通过Spring提供的工具类获取ApplicationContext对象
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc); ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); ac1.getBean("beanId"); ac2.getBean("beanId");
说明:通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。
3、实现接口ApplicationContextAware
说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。Spring初始化时,会通过该方法将ApplicationContext对象注入。
package com.liu.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/** * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量. */ public void setApplicationContext(ApplicationContext applicationContext) { SpringContextUtil.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"); } }
}
4、通过Spring提供的ContextLoader
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
wac.getBean(beanID);
说明:注意一点,在服务器启动时,Spring容器初始化时,不能通过以上方法获取Spring 容器。
5、继承自抽象类ApplicationObjectSupport
说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取ApplicationContext。
Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext 对象注入。
6、继承自抽象类WebApplicationObjectSupport
说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext
相关文章推荐
- spring获取bean的几种方法
- Spring Bean装配--Resource(获取文件的几种常用方法)
- springmvc(基础五) Spring3 mvc 获取请求参数的几种方式
- C#基础:获取程序所在路径的几种方法
- Spring获取Bean的几种方式
- 几种获取spring环境上下文方法
- Spring获取Bean的几种方式
- Spring获取Bean的几种方式 分享
- spring获取webapplicationcontext,applicationcontext几种方法详解
- Spring2.5.6学习笔记-实例化bean的几种方法
- 获取Spring 容器中bean的方法
- 几种获得spring里注册Bean的方法
- spring 实例化bean的几种方法
- 几种获得spring里注册Bean的方法
- spring 项目中获取bean 的方法
- spring获取webapplicationcontext,applicationcontext几种方法详解
- Spring获取Bean的几种方式
- 几种获得spring里注册Bean的方法
- [收藏]spring获取webapplicationcontext,applicationcontext几种方法详解
- spring中读取xml配置文件、获取bean的几种方式