spring 缺省:

    • 1.spring用DefaultListableBeanFactory.preInstantiateSingletons()建立bean实例
    • 2.缺省采用单例模式

在最近的项目中,有个地方我们不得不实用getBean的方法,自己从Spring context中获取bean进行数据库操作。

方法一(效率低,极易出现bug,不推荐使用):

刚刚开始的时候,我们使用这中方式,但是在应用过程中发现此方式效率低下,而且极易出现bug。
在我们系统中会生成ehcache_auto_created_时间戳文件夹,

String[] xmlCfg = new String[] {"classpath:/spring/applicationContext-service.xml",
"classpath:/spring/applicationContext-util.xml",
"classpath:/spring/applicationContext.xml"};
ApplicationContext context = new FileSystemXmlApplicationContext(xmlCfg);
// 获取inspectionUtil bean
inspectionUtil = (InspectionUtil) context.getBean("inspectionUtil");

所以我google了一下,改用其他方法。

方法二(效率高,灵活性高,可复用,推荐使用):
创建一个工具类SpringContextsUtil
,通过实现Spring中的ApplicationContextAware接口,在applicationContext.xml中注入bean后
Spring会自动调用setApplicationContext方法。此时我们就可以获取到Spring context。

public class SpringContextsUtil implements ApplicationContextAware{

private static ApplicationContext applicationContext;    //Spring应用上下文环境
/**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
* @param applicationContext
* @throws BeansException
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextsUtil.applicationContext = applicationContext;
} /**
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
} /**
* 获取对象
* @param name
* @return Object 一个以所给名字注册的bean的实例
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
} /**
* 获取类型为requiredType的对象
* 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException)
* @param name bean注册名
* @param requiredType 返回对象类型
* @return Object 返回requiredType类型对象
* @throws BeansException
*/
public static Object getBean(String name, Class requiredType) throws BeansException {
return applicationContext.getBean(name, requiredType);
} /**
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
* @param name
* @return boolean
*/
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
} /**
* 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
* 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
* @param name
* @return boolean
* @throws NoSuchBeanDefinitionException
*/
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
} /**
* @param name
* @return Class 注册对象的类型
* @throws NoSuchBeanDefinitionException
*/
public static Class getType(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getType(name);
} /**
* 如果给定的bean名字在bean定义中有别名,则返回这些别名
* @param name
* @return
* @throws NoSuchBeanDefinitionException
*/
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getAliases(name);
}
}
// 获取inspectionUtil bean
inspectionUtil = (InspectionUtil) SpringContextUtil.getBean("inspectionUtil");

注:

1、使用时会出现无法获取applicationContext,并抛出NullPointerException。
原因:使用此方法必须在spring applicationContext.xml中注入bean。否则spring无法自动调用setApplicationContext。如下

<bean id="springContextsUtil" class="com.sinosoft.sepmis.util.SpringContextsUtil" ></bean>
2、如果注入后仍然出现这个问题。
则修改 中的default-lazy-init=“false”。
或者是修改bean注入属性
<bean id="springContextsUtil" class="com.sinosoft.sepmis.util.SpringContextsUtil" lazy-init="false">

spring getbean 方法分析的更多相关文章

  1. spring getbean 方法分析(很实用!)

    十年阿里,就只剩下这套Java开发体系了 >>>   在最近的项目中,有个地方我们不得不实用getBean的方法,自己从Spring context中获取bean进行数据库操作. 方 ...

  2. spring容器的refresh方法分析

    spring源码版本5.0.5 Spring容器创建之后,会调用它的refresh方法刷新Spring应用的上下文. 首先整体查看AbstractApplicationContext#refresh源 ...

  3. Spring IoC getBean 方法详解

    前言 本篇文章主要介绍 Spring IoC 容器 getBean() 方法. 下图是一个大致的流程图: 正文 首先定义一个简单的 POJO,如下: public class User { priva ...

  4. Spring BeanFacoty doCreateBean方法分析

    上一篇,我们分析到了doCreateBean,现在继续: 先看看时序图 protected Object doCreateBean(final String beanName, final RootB ...

  5. 模拟Spring中的getBean方法

    一直知道Spring是运用反射技术的,但具体怎么用呢?今天就模拟下getBean方法. 步骤: 1.用Dom4j解析xml配置文件,取出我们需要的信息 2.遍历Bean节点,根据每个Bean节点的cl ...

  6. 模拟Spring容器的getBean方法(Maven工程)

    Spring容器的getBean方法是通过反射机制实现的,下面的测试程序模拟getBean的实现原理. 步骤一:pom.xml文件配置解析XML文件的dom4j.jar 步骤二:XML文件中配置bea ...

  7. Spring之BeanFactory:解析getBean()方法

    初探getBean()方法 在使用Spring的时候可以通过如下方式调用getBean方法来获取某个Bean: User user = context.getBean(User.class); Abs ...

  8. Spring源码分析——BeanFactory体系之抽象类、类分析(二)

    上一篇分析了BeanFactory体系的2个类,SimpleAliasRegistry和DefaultSingletonBeanRegistry——Spring源码分析——BeanFactory体系之 ...

  9. Spring源码分析——BeanFactory体系之接口详细分析

    Spring的BeanFactory的继承体系堪称经典.这是众所周知的!作为Java程序员,不能错过! 前面的博文分析了Spring的Resource资源类Resouce.今天开始分析Spring的I ...

随机推荐

  1. APT攻防对抗

    APT(高级持续性威胁)攻击是指近年来,专业甚至是有组织和国家背景支持的黑客,针对重要目标和系统发起的一种攻击手段,主要特征有 1)持续性:攻击者为了重要的目标长时间持续攻击直到攻破为止.攻击成功用上 ...

  2. C#语法中一个问号(?)和两个问号(??)的运算符是什么意思?

    (1).C#语法中一个个问号(?)的运算符是指:可以为 null 的类型. MSDN上面的解释: 在处理数据库和其他包含不可赋值的元素的数据类型时,将 null 赋值给数值类型或布尔型以及日期类型的功 ...

  3. 接口 --- Java

    package com.test2; public class Test { public static void main(String[] args) { // TODO Auto-generat ...

  4. mysql select count(filed) 问题(where条件没有数据匹配的话也有数据返回)。

    问题: SELECT count(*),user_id FROM tb_rp_logintrace WHERE id=-1 返回结果: count(*), user_id 0             ...

  5. C# zip压缩

    /**//* * Gary Zhang -- cbcye@live.com * www.cbcye.com * www.quicklearn.cn * cbcye.cnblogs.com */ usi ...

  6. Dell笔记本禁用触摸板的方法

    一·找到触摸板驱动所在的文件夹(其他型号 的本本,请自己探索一下,找到驱动在哪就行),一般 在 C:\program files\delltpad 中(若没有请下载安 装 ),如图: 二·双击 Del ...

  7. 【转】NI语法 JNI参考 JNI函数大全

    原文网址:http://blog.sina.com.cn/s/blog_5de73d0b0101chk1.html 一.对照表 Java类型    本地类型         描述boolean     ...

  8. 18款 非常实用 jquery幻灯片图片切换

    1.jquery图片滚动仿QQ商城带左右按钮控制焦点图片切换滚动 jquery图片特效制作仿腾讯QQ商城首页banner焦点图片轮播切换效果,带索引按钮控制和左右按钮控制图片切换. 查看演示>& ...

  9. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.1.2

    Let $X$ be nay basis of $\scrH$ and let $Y$ be the basis biorthogonal to it. Using matrix multiplica ...

  10. hibernate之参数绑定

    hibernate之参数绑定 ---------- 我们应该拒绝SQL(或HQL)的拼装,应该永远不要编写这样的代码,有这很严重的安全问题,众所周知的SQL注入.我们可以考虑参数绑定,在hiberna ...