获得spring里注册Bean的有好几种方法,这里介绍一种比较简单的方法:

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
/**
 *功能描述:获取spring容器,以访问容器中定义的其他bean 
 */
  /**  
 
public class SpringContextUtil implements ApplicationContextAware {

/** 
  * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. 
  *   
  *  
  */
        private static ApplicationContext applicationContext; //Spring应用上下文环境   
 
        /**  
         * 实现ApplicationContextAware接口的回调方法,设置上下文环境     
         * @param applicationContext  
         * @throws BeansException  
         */
        public void setApplicationContext(ApplicationContext applicationContext)
                        throws BeansException {
                SpringContextUtil.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);
        }
}

-------------------
调用:

UserService userService = (UserService) SpringContextUtil.getBean("userService");

一种获取spring环境上下文方法:SpringContextUtil的更多相关文章

  1. 十二种获取Spring的上下文环境ApplicationContext的方法

    转载:https://my.oschina.net/u/2391658/blog/729414

  2. 获取spring容器上下文(webApplicationContext)的几种方法

    在很多情况,我们需要先获取spring容器上下文,即webApplicationContext,然后通过webApplicationContext来获取其中的bean.典型的情况是,我想在一个并未委托 ...

  3. 获取Spring的上下文环境ApplicationContext的方式

    摘自: http://blog.csdn.net/yang123111/article/details/32099329 获取Spring的上下文环境ApplicationContext的方式 Web ...

  4. 怎么随时获取Spring的上下文ApplicaitonContext,和Spring管理的Bean

    BeanFactory接口 Interface BeanFactory getBean <T> T getBean(String name, Class<T> required ...

  5. 介绍几种搭建Dojo环境的方法

    Hello World! 的时间到了,在你所学过的众多语言中,哪个不是从此学起的呢?但在此之前,我们要先构建一个开发环境,如同刚开始学习Java的时候,还是需要我们先安装JDK.配置好环境变量等等,H ...

  6. php 两种获取分类树的方法

    php 两种获取分类树的方法 1. /** * 获取分类树 * @param array $array 数据源 * @param int $pid 父级ID * @param int $level 分 ...

  7. 【Spring】非Spring IOC容器下获取Spring IOC上下文的环境

    前言 在Spring Web项目中,有些特殊的时候需要在非Spring IOC容器下获取Spring IOC容器的上下文环境,比如获取某个bean. 版本说明 声明POM文件,指定需引入的JAR. & ...

  8. 获取Spring应用环境上下文bean

    import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBean ...

  9. 获取spring容器对象方法和原因

    为什么要获取Spring容器对象:拿到spring容器对象后,你就可以用spring管理的bean了,拿到bean,自然可以使用bean的方法,场景:比如jsp页面.通过注解是无法注入bean的,在开 ...

随机推荐

  1. ie textarea不支持maxlength textarea限制长度

    //ie textarea不支持maxlength $('#verify_note').bind('input propertychange', function() { if (this.value ...

  2. 【Lucene4.8教程之五】Luke

    一.Luke基本内容 1.Luke简介 Luke可用于查看Lucene创建的索引,并对其进行基本操作. 2.创建Luke (1)从Github上下载源文件 https://github.com/tar ...

  3. android listview Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3

    android listview 适配器在多种类型viewType报错: Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; ...

  4. https://github.com/aptana/studio3/releases aptana

    https://github.com/aptana/studio3/releases  aptana

  5. pubwin2009服务端 修改系统时间方法

      所有操作都是必须的,包括重启. 1.建议操作前全体结账,并交班---不结帐的话, 改完时间会出问题的.2.停止pubwin2009服务端 3.将C:\WINDOWS\system32\driver ...

  6. Ubuntu14.04安装PostpreSQL9.3.5记录

    安装参考:http://www.postgresql.org/download/linux/ubuntu/ y@y:~$ sudo apt-get install postgresql-9.3 pos ...

  7. Uubntu scrot 的简单使用

    scrot 是一个使用 imlib2 库截取屏幕和保存图像的的工具. 1:安装 #apt-get install scrot 2:查看帮助 #scrot -help -v, --version显示版本 ...

  8. SqlServer sys.partition_view

    --查看partition的四个视图 select * from sys.partition_functions--查看分区函数 select * from sys.partition_paramet ...

  9. 百度地图LV1.5实践项目开发工具类bmap.util.jsV1.1

    /** * 百度地图使用工具类-v1.5 * * @author boonya * @date 2013-7-7 * @address Chengdu,Sichuan,China * @email b ...

  10. Jquery时间验证和转换工具

    var TimeObjectUtil; /** * @title 时间工具类 * @note 本类一律违规验证返回false * @author {boonyachengdu@gmail.com} * ...