代码先锋网 代码片段及技术文章聚合

springboot实战 获取spring上下文的4种方式

技术标签: spring boot  spring  spring boot  bean  ioc

实际开发中我们经常需要通过spring上下文获取一些配置信息,本文阐述springboot应用获取spring上下文的几种方式。

方式一:实现ApplicationContextAware接口

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

/**
 * 实现ApplicationContextAware接口设置applicationContext
 * 提供static方法供调用者使用,不要求使用者受spring容器管理
 */
@Component
public class SpringContextUtil1 implements ApplicationContextAware {

    public static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil1.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return SpringContextUtil1.applicationContext;
    }

    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}

方式二:非static方法版

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

/**
 * 实现ApplicationContextAware接口设置applicationContext
 * 如果使用者也是被spring管理的bean则可以使用注入的方式使用,而非调用static方法
 */
@Component
public class SpringContextUtil2 implements ApplicationContextAware {

    public ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public ApplicationContext getApplicationContext() {
        return this.applicationContext;
    }

    public Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    public <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    public <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}


@Component
public class ContextUser{

    /**
     * 使用者直接注入
     */
    @Autowired
    public SpringContextUtil2 springContextUtil2;
 
}

方式三:在springboot引导类里设置

import org.springframework.context.ApplicationContext;

/**
 * 在springboot引导类里设置applicationContext
 * 工具类无需实现ApplicationContextAware接口
 */

public class SpringContextUtil3 {

    public static ApplicationContext applicationContext;

    public static ApplicationContext getApplicationContext() {
        return SpringContextUtil3.applicationContext;
    }

    public static void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextUtil3.applicationContext = applicationContext;
    }

    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    public static Object getBean(Class<?> requiredType) {
        return getApplicationContext().getBean(requiredType);
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }
}


//在springboot引导类里设置applicationContext
@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);
        SpringContextUtil3.setApplicationContext(context);
    }
}

方式四:直接注入ApplicationContext

@Component
public class ContextUser{

    /**
     * 在需要使用上下文的地方直接注入(前提:使用者受spring容器管理)
     */
   @Autowired
   public ApplicationContext applicationContext;
 
}
  

版权声明:本文为agonie201218原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/agonie201218/article/details/106714103

智能推荐

springboot的应用上下文

springboot上下文有两个 ServletWebServerApplicationContext AnnotationConfigServletWebServerApplicationContext(继承上面) ServletWebServerApplicationContext 该类属于spring-boot-2.1.1Release.jar中,是自springboot诞生就衍生出来的,是...

springboot中通过上下文来获取任何Spring注入类实体

springboot中通过上下文来获取任何Spring注入类实体 实现ApplicationContextAware来完成 2.通过自带类 WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext); 其中 servletContext ...

Spring实战学习-使用上下文和bean的生命周期

上下文的几种获取方法: 1.AnnotationConfigApplicationContext:从一个或多个基于Java文件的配置类中加载Spring应用上下文。 2.FileSystemXmlApplicationContext:从文件系统路径下的xml配置文件中加载应用上下文。 3.ClassPathXmlApplicationContext:从类路径下xml配置文件中加载上下文定义,把应用...

Spring获取上下文的四种方式方式

一、使用ApplicationContextInitializer,让SpringApplication启动时进行回调,然后获取ApplicationContext 1.创建一个SpringContextHolder对象用来保存ApplicationContext对象 2.在启动类中注册ApplicationContextInitializer接口的匿名对象 二、SpringApplication...

springboot 使用上下文获取bean

springboot-使用上下文获取bean 在使用springboot开发项目过程中,有些时候可能出现说会有在spring容器加载前就需要注入bean的类,这个时候如果直接使用@Autowire注解,则会出现控制针异常! 解决办法如下: 创建一个springContextUtil类 在AppcloudApplication.class 启动类里边,将初始化该类,并将context注入进去 在需要...

猜你喜欢

SpringBoot在页面获取项目上下文路径

1. 首先在配置文件application.properties中添加下面的配置:  2. 然后在页面中通过${request.contextPath}, 例如:   ...

springboot静态方法中获取上下文对象--ConfigurableApplicationContext

一.启动类: 二、添加SpringBeanUtils工具类 三、静态方法获取Bean对象 四、使用getBeanByString()方法,通过bean名获取Bean对象 五、使用getBean()方法,通过.class()获取Bean对象 六、留言: 欢迎留言更好的方法!...

在SpringBoot中如何获取上下文容器,拿到IOC中的Bean

过实现ApplicationContextAware接口,重写setApplicationContext即可获得当前使用的ApplicationContext容器对象...

Spring进阶(一)SpringBoot替换上下文中的Bean

文章目录 业务场景 一、使用AOP为使用到方法添加自定义注解 二、在系统启动时直接替换 实现方式 思路 具体代码 目录结构 DemoAnnotaion DemoApplicationListener DemoMethodInterceptor TestService DemoController DemoApplication 启动测试 业务场景 已经写好业务入库的代码,现在要在数据库添加了一个公...