springboot 通过 ApplicationContextAware、ApplicationContext获取spring管理的bean

spring 使用xml、注解的形式装配Bean

我们可以通过注解@Autowired 很简单方便获取bean,如下:

@Autowired
private Test test;

虽然这种方法很简单方便,但是有些特殊场景用不了。

借助ConfigurableApplicationContext获取bean

还可以用 springboot启动类 SpringApplication.run 返回的 ConfigurableApplicationContext对象的getBean 获取想要的bean, ConfigurableApplicationContext 继承自 ApplicationContext。
通过启动类ConfigurableApplicationContext 对象geBean, 如果不在在启动类中用起来就有点小麻烦,虽然可以做到。

借助ApplicationContextAware获取bean

spring加载配置文件时,会自动调用ApplicationContextAware中的setApplicationContext
我们可以实现一个工具类继承ApplicationContextAware并重写setApplicationContext 获取applicationContext 保存到工具类中,通过注解@Component 或其他 将工具类bean交给spring管理创建。这以后我们就能够方便在我们想要的地方通过applicationContext 获取想要的bean了。

具体实现代码

package com.example.applicationcontextutil_demo.util;

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

@Component
public class ApplicationContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    /**
     * 实现ApplicationContextAware接口, 注入Context到静态变量中.
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextUtil.applicationContext = applicationContext;
    }

    /**
     * 获取静态变量中的ApplicationContext.
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        return (T) applicationContext.getBean(name);
    }

    /**
     * 从静态变量applicationContext中得到Bean, 自动转型为所赋值对象的类型.
     */
    public static <T> T getBean(Class<T> requiredType) {
        return applicationContext.getBean(requiredType);
    }
}

下面是一个测试demo源码

目录结构
在这里插入图片描述
pom.xml 依赖就一个web包

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

application.properties 端口被占用了随便选了个 8091

server.port=8091

TestServiceImpl

package com.example.applicationcontextutil_demo.service;

import org.springframework.stereotype.Service;

@Service
public class TestServiceImpl {

    public String hello(){
        return "hello world";
    }

}

TestController

package com.example.applicationcontextutil_demo.controller;

import com.example.applicationcontextutil_demo.service.TestServiceImpl;
import com.example.applicationcontextutil_demo.util.ApplicationContextUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping("/test")
    public String test(){
        //注意getBean(String s) bean在spring中对象名小写开头
        return ((TestServiceImpl)ApplicationContextUtil.getBean("testServiceImpl")).hello();
    }

    @RequestMapping("/test2")
    public String test2(){
        return ApplicationContextUtil.getBean(TestServiceImpl.class).hello();
    }

}

测试

浏览器输入 127.0.0.1:8091/test
在这里插入图片描述
浏览器输入 127.0.0.1:8091/test2
在这里插入图片描述

源码

链接:https://pan.baidu.com/s/1P20wO2Gj6X2rM17DMIu6fw
提取码:di9o

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值