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 ...
随机推荐
- guava常用集合交集,差集,并集,补集操作
<!-- https://mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <grou ...
- 无法下载golang.org-x-net解决方法
由于go的很多包都依赖了google官方的包,而google官方的包都在google服务器上,因为某些原因无法直接访问,在搜索了很多解决方案后,找到了最简单的一个方法: 1. 找到对应包在github ...
- 014-操作系统下验证下载文件的 MD5/SHA1/SHA256
一.mac 1.md5 openssl md5 /path/to/file 新的macOS默认支持:md5 filename 2.sha256 openssl dgst -sha256 /path/t ...
- 18 Flutter仿京东商城项目 商品详情顶部tab切换 顶部下拉菜单 底部浮动导航
ProductContent.dart import 'package:flutter/material.dart'; import '../services/ScreenAdaper.dart'; ...
- 通过id获取指定元素内容(标签里面的 标签内容获取)
html页面如下 <tr style="background-color:#fff;"> <td colspan="2" align=left ...
- leetcode刷题-559. Maximum Depth of N-ary Tree
题目: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/ n-ary-tree的数据结果表示 // Defi ...
- JAVA 基础编程练习题17 【程序 17 猴子吃桃问题】
17 [程序 17 猴子吃桃问题] 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又 将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前一天剩下的一 ...
- PhpStorm设置项目编码
因为工作中论坛项目用的是GBK编码.数据库也是GBK编码.模板也是GBK,所以为了以后修改程序不出现乱码问题,所以需要把我的PhpStorm中该项目也设置为GBK编码(默认是UTF8编码). 设置路径 ...
- Flutter中通过循环渲染组件
class ContactsState extends State<Contacts>{ List formList; initState() { super.initState(); f ...
- 退出virtual box 独占键盘和鼠标
先按住右边的Alt键,然后按一下(右边)ctrl键