Spring 自定义Bean 实例获取
一、通过指定配置文件获取, 对于Web程序而言,我们启动spring容器是通过在web.xml文件中配置,这样相当于加载了两次spring容器
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");
二、通过Spring提供的工具类获取ApplicationContext对象
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
MyService service1 = (MyService)ac1.getBean("bean1");//这是beanId.
MyService service2 = (MyService)ac2.getBean("bean2");
这种方式明显有很大的漏洞,其一:需要request对象,其二:很难封装一个Java工具类
三、实现接口ApplicationContextAware, 或继承实现ApplicationContextAware接口的类
package com.zxguan; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ApplicationObjectSupport; /**
* @author zxguan
* @description
* @create 2018-01-29 14:54
*/
public class SpringTool extends ApplicationObjectSupport { private static ApplicationContext applicationContext = null; @Override
protected void initApplicationContext(ApplicationContext context) throws BeansException {
super.initApplicationContext(context);
if (null == SpringTool.applicationContext) {
SpringTool.applicationContext = context;
}
} public static ApplicationContext getAppContext() {
return applicationContext;
} public static Object getBean(String beanId){
return getAppContext().getBean(beanId);
}
}
还需将类 SpringTool 交由 Spring容器管理
Spring 自定义Bean 实例获取的更多相关文章
- Spring内部bean实例
在Spring框架中,一个bean仅用于一个特定的属性,这是提醒其声明为一个内部bean.内部bean支持setter注入“property”和构造器注入"constructor-arg“. ...
- spring自定义bean工厂模式解耦
在resources下创建bean.properties accountService=cn.flypig666.service.impl.AccountServiceImpl accountDao= ...
- Spring中Bean实例的生命周期及其行为
- Spring三 Bean的三种创建方式
创建Bean的三种方式在大多数情况下,Spring容器直接通过new关键字调用构造器来创建Bean实例,而class属性指定Bean实例的实现类,但这不是实例化Bean的唯一方法.实际上,Spring ...
- 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法
Java提供了Class类,可以通过编程方式获取类别的字段和方法,包括构造方法 获取Class类实例的方法: 类名.class 实例名.getClass() Class.forNam ...
- SpringBoot 注册拦截器方式及拦截器如何获取spring bean实例
SpringBoot 注册拦截器时,如果用New对象的方式的话,如下: private void addTokenForMallInterceptor(InterceptorRegistry regi ...
- 获取Spring容器中Bean实例的工具类(Java泛型方法实现)
在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...
- Spring容器中获取bean实例的方法
// 得到上下文环境 WebApplicationContext webContext = ContextLoader .getCurrentWebApplicationContext(); // 使 ...
- Spring源码浅析之bean实例的创建过程(二)
在上一篇内容中,介绍了doGetBean方法的源码内容,知道了bean在创建的过程中,有三个范围,单例.多例.Scope,里面都使用到了createBean.下面本篇文章的主要内容,就是围绕creat ...
随机推荐
- Ubuntu下Nginx的安装和卸载
环境是Ubuntu 16.04 安装: sudo apt-get update sudo apt-get install nginx 卸载: sudo apt-get --purge remove n ...
- Redis监控之redis-stat安装与详解
一.安装环境 安装编译环境.ruby运行环境.git代码 yum install gcc-c++ yum -y install ruby-devel yum install ruby yum inst ...
- python操作MySQL数据库的三个模块
python使用MySQL主要有两个模块,pymysql(MySQLdb)和SQLAchemy. pymysql(MySQLdb)为原生模块,直接执行sql语句,其中pymysql模块支持python ...
- [转]用代码访问 Https
可以参考: https://blog.csdn.net/irokay/article/details/78801307 跳过证书验证方法 HttpClient简介HTTP 协议可能是现在 Intern ...
- Servlet的三种实现方式
A: 实现Servlet接口 B: 继承GenericServlet C: 继承HttpServlet,它是对Http协议进行了封装
- android在点击EditText的时候始终不弹出软件键盘
场景描述:正常情况下,当点击EditText时,软键盘会弹出来.现在的要求是当点击EditText时,弹日期选择对话框,选择的结果显示在EditText上.若不处理,当点击EditText时,软键盘和 ...
- qt application logging
“AnalysisPtsDataTool201905.exe”(Win32): 已加载“F:\OpencvProject\ZY-Project\x64\Debug\AnalysisPtsDataToo ...
- Selenium下Chrome配置 (含启动无痕界面)
例子: 设置无界面模式浏览器启动chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') ...
- 科普贴:什么是上证50ETF期权?如何交易?
这两天很多上证50ETF期权即将开通的新闻,有几个朋友表示,看了很多新闻,同样还是云里雾里,没搞清楚究竟是个什么东东?今天科普一下,部分内容摘自网络. 1.什么是ETF?ETF的英文全称是:Excha ...
- swift 第六课 scrollview xib 的使用
现在 xib,stroyBoard 这种图形话的编辑写代码,越来越简单.以前scrollview 这样的控件不会用xib ,网上查了 好多的资料.现在把步骤逐渐的写出来, 这里顺便写个Demo ,是一 ...