详解SpringBoot静态方法获取bean的三种方式

 更新时间:2021年10月22日 09:29:12   作者:chilx  
本文主要介绍了详解SpringBoot静态方法获取bean的三种方式,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

方式一  注解@PostConstruct

import com.example.javautilsproject.service.AutoMethodDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
 
/**
 * springboot静态方法获取 bean 的三种方式(一)
 * @author: clx
 * @date: 2019/7/23
 * @version: 1.1.0
 */
@Component
public class StaticMethodGetBean_1 {
 
    @Autowired
    private AutoMethodDemoService autoMethodDemoService;
 
    @Autowired
    private static AutoMethodDemoService staticAutoMethodDemoService;
 
    @PostConstruct
    public void init() {
        staticAutoMethodDemoService = autoMethodDemoService;
    }
 
    public static String getAuthorizer() {
        return staticAutoMethodDemoService.test();
    }
}

注解@PostConstruct说明

PostConstruct 注释用于在依赖关系注入完成之后需要执行的方法上,以执行任何初始化。此方法必须在将类放入服务之前调用。支持依赖关系注入的所有类都必须支持此注释。即使类没有请求注入任何资源,用 PostConstruct 注释的方法也必须被调用。只有一个方法可以用此注释进行注释。

应用 PostConstruct 注释的方法必须遵守以下所有标准:

  • 该方法不得有任何参数,除非是在 EJB 拦截器 (interceptor) 的情况下,根据 EJB 规范的定义,在这种情况下它将带有一个 InvocationContext 对象 ;
  • 该方法的返回类型必须为 void;
  • 该方法不得抛出已检查异常;
  • 应用 PostConstruct 的方法可以是 public、protected、package private 或 private;
  • 除了应用程序客户端之外,该方法不能是 static;
  • 该方法可以是 final;
  • 如果该方法抛出未检查异常,那么不得将类放入服务中,除非是能够处理异常并可从中恢复的 EJB。

方式二  启动类ApplicationContext

实现方式:在springboot的启动类中,定义static变量ApplicationContext,利用容器的getBean方法获得依赖对象

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
 * @author: clx
 * @date: 2019/7/23
 * @version: 1.1.0
 */
@SpringBootApplication
public class Application {
    public static ConfigurableApplicationContext ac;
    public static void main(String[] args) {
       ac = SpringApplication.run(Application.class, args);
    } 
}

调用方式

/**
 * @author: clx
 * @date: 2019/7/23
 * @version: 1.1.0
 */
@RestController
public class TestController {
    /**
     * 方式二
     */
    @GetMapping("test2")
    public void method_2() {
        AutoMethodDemoService methodDemoService = Application.ac.getBean(AutoMethodDemoService.class);
        String test2 = methodDemoService.test2();
        System.out.println(test2);
    }
}

方式三 手动注入ApplicationContext

手动注入ApplicationContext

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
 
/**
 * springboot静态方法获取 bean 的三种方式(三)
 * @author: clx
 * @date: 2019/7/23
 * @version: 1.1.0
 */
@Component
public class StaticMethodGetBean_3<T> implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        StaticMethodGetBean_3.applicationContext = applicationContext;
    }
 
    public static <T> T  getBean(Class<T> clazz) {
        return applicationContext != null?applicationContext.getBean(clazz):null;
    }
}

调用方式

    /**
     * 方式三
     */
    @Test
    public void method_3() {
        AutoMethodDemoService autoMethodDemoService = StaticMethodGetBean_3.getBean(AutoMethodDemoService.class);
        String test3 = autoMethodDemoService.test3();
        System.out.println(test3);
    }

以上三种方式楼主都测试过可以为完美使用

到此这篇关于详解SpringBoot静态方法获取bean的三种方式的文章就介绍到这了,更多相关SpringBoot静态方法获取bean内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring Boot设置支持跨域请求过程详解

    Spring Boot设置支持跨域请求过程详解

    这篇文章主要介绍了Spring Boot设置支持跨域请求过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • SpringBoot 请求参数忽略大小写的实例

    SpringBoot 请求参数忽略大小写的实例

    这篇文章主要介绍了SpringBoot 请求参数忽略大小写的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • SpringBoot使用classfinal-maven-plugin插件加密Jar包的示例代码

    SpringBoot使用classfinal-maven-plugin插件加密Jar包的示例代码

    这篇文章给大家介绍了SpringBoot使用classfinal-maven-plugin插件加密Jar包的实例,文中通过代码示例和图文讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-02-02
  • Java Hashtable机制深入了解

    Java Hashtable机制深入了解

    HashTable是jdk 1.0中引入的产物,基本上现在很少使用了,但是会在面试中经常被问到。本文就来带大家一起深入了解一下Hashtable,需要的可以参考一下
    2022-09-09
  • Java 延时队列及简单使用方式详解

    Java 延时队列及简单使用方式详解

    这篇文章主要介绍了Java延时队列简单使用方式,通过本文学习知道延时队列是什么可以用来干什么,本文通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-08-08
  • SpringBoot 实战 之 优雅终止服务的方法

    SpringBoot 实战 之 优雅终止服务的方法

    本篇文章主要介绍了SpringBoot 实战 之 优雅终止服务的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • J2SE基础之命令行中编写第一个 Hello World

    J2SE基础之命令行中编写第一个 Hello World

    “Hello World”程序指的是只在计算机屏幕上输出“Hello, World!”(意为“世界,你好!”)这行字符串的计算机程序。hello world作为所有编程语言的起始阶段,占据着无法改变的地位,所有的编程第一步就在于此了!经典之中的经典!hello world!
    2016-05-05
  • java项目中的绝对路径和相对路径用法说明

    java项目中的绝对路径和相对路径用法说明

    这篇文章主要介绍了java项目中的绝对路径和相对路径用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • SpringBoot集成RocketMQ实现消息发送的三种方式

    SpringBoot集成RocketMQ实现消息发送的三种方式

    RocketMQ 支持3 种消息发送方式: 同步 (sync)、异步(async)、单向(oneway),本文就将给大家介绍一下SpringBoot集成RocketMQ实现消息发送的三种方式文中有详细的代码示例,需要的朋友可以参考下
    2023-09-09
  • java stream使用指南之sorted使用及进阶方式

    java stream使用指南之sorted使用及进阶方式

    这篇文章主要介绍了java stream使用指南之sorted使用及进阶方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05

最新评论