前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【springMVC基础】spring获取bean的几种方法

【springMVC基础】spring获取bean的几种方法

作者头像
用户5640963
发布2019-07-25 15:32:39
2.3K0
发布2019-07-25 15:32:39
举报
文章被收录于专栏:卯金刀GG

1、ApplicationContext对象

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;

代码语言:javascript
复制
/**
 * 实现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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档