SpringBoot项目中获取applicationContext对象

  ApplicationContext 对象是Spring开源框架的上下文对象实例,也就是我们常说的Spring容器,一般情况下我们是不用手动来管理它,而是由Spring框架自己来维护bean之间的关系,但不排除我们有的时候需要手动获取Spring容器(比如获取多例bean的时候),传统的获取ApplicationContext 的方式有很多种,这里介绍官方推荐的方式:使用ApplicationContextAware接口。

  其实以实现ApplicationContextAware接口的方式获取ApplicationContext 对象实例并不是SpringBoot特有的功能,早在Spring3.0x版本之后就存在了这个接口,在传统的Spring项目内同样是可以以实现ApplicationContextAware接口的方式获取到ApplicationContext 实例的。具体使用代码如下:

  

package com.example.demo;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class MyApplicationContext implements ApplicationContextAware {
    /**
     * 上下文对象实例
     */
    private ApplicationContext applicationContext;

    
    /**
     * 实现ApplicationContextAware接口的方法
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 获取applicationContext
     * 
     * @return
     */
    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过bean的名字获取 Bean.
     * 
     */
    public Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过类名获取Bean.
     * 
     */
    public <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

}

  在MyApplicationContext上是有@Component注解的,Spring会自动调用setApplicationContext方法来为我们设置上下文实例,所以我们可以在需要获取上下文的实体内中注入MyApplicationContext对象,进而使用其中的方法来获取上下文对象等。如果想在非Spring管理的实体内使用ApplicationContext,这时我们可以修改ApplicationContext实例对象为静态实例,方法改为静态方法,这样在外部同样是可以获取到指定Bean的实例,示例如下:

package com.example.demo;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class MyApplicationContextUtil implements ApplicationContextAware {
    /**
     * 上下文对象实例
     */
    private static ApplicationContext myApplicationContext;

    /**
     * 实现ApplicationContextAware接口的方法
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        myApplicationContext = applicationContext;
    }

    /**
     * 获取applicationContext
     * 
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return myApplicationContext;
    }

    /**
     * 通过bean的名字获取 Bean.
     * 
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过类名获取Bean.
     * 
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }
}




猜你喜欢

转载自www.cnblogs.com/hhhshct/p/9669961.html