作者 | chilx

来源 | https://blog.csdn.net/showchi/article/details/97005720

注意:调用者要被spring管理

方式一

注解@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* @version: 1.1.0*/
@Component
public class StaticMethodGetBean_1 {@Autowiredprivate AutoMethodDemoService autoMethodDemoService;@Autowiredprivate static AutoMethodDemoService staticAutoMethodDemoService;@PostConstructpublic void init() {staticAutoMethodDemoService = autoMethodDemoService;}public static String getAuthorizer() {return staticAutoMethodDemoService.test();}
}

PostConstruct 注释用于在依赖关系注入完成之后需要执行的方法上,以执行任何初始化。此方法必须在将类放入服务之前调用。

支持依赖关系注入的所有类都必须支持此注释。即使类没有请求注入任何资源,用 PostConstruct 注释的方法也必须被调用。只有一个方法可以用此注释进行注释。

如果您正在学习Spring Boot,推荐一个连载多年还在继续更新的免费教程:http://blog.didispace.com/spring-boot-learning-2x/

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

  • 该方法不得有任何参数,除非是在 EJB 拦截器 (interceptor) 的情况下,根据 EJB 规范的定义,在这种情况下它将带有一个 InvocationContext 对象 ;

  • 该方法的返回类型必须为 void;

  • 该方法不得抛出已检查异常;

  • 应用 PostConstruct 的方法可以是 public、protected、package private 或 private;

  • 除了应用程序客户端之外,该方法不能是 static;

  • 该方法可以是 final;

  • 如果该方法抛出未检查异常,那么不得将类放入服务中,除非是能够处理异常并可从中恢复的 EJB。

  • Spring Boot 学习笔记,这个太全了!

方式二

启动类ApplicationContext

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

如果您正在学习Spring Boot,推荐一个连载多年还在继续更新的免费教程:http://blog.didispace.com/spring-boot-learning-2x/

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/*** @author: clx* @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* @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

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;/*** springboot静态方法获取 bean 的三种方式(三)* @author: clx* @version: 1.1.0*/
@Component
public class StaticMethodGetBean_3<T> implements ApplicationContextAware {private static ApplicationContext applicationContext;@Overridepublic 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;}
}

另外,如果您正在学习Spring Cloud,推荐一个连载多年还在继续更新的免费教程:https://blog.didispace.com/spring-cloud-learning/

调用方式

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

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

(完)

往期推荐

为什么IDEA不推荐你使用@Autowired ?

GitHub高赞,一款足以取代迅雷的开源下载工具

炸裂!跑P站上教微积分,年入170w...

网传字节跳动、腾讯将执行1075和965工作制?加班要审批才行!

后端开挂:3行代码 = 8个接口

技术交流群

最近有很多人问,有没有读者交流群,想知道怎么加入。加入方式很简单,有兴趣的同学,只需要点击下方卡片,回复“加群“,即可免费加入我们的高质量技术交流群!

点击阅读原文,送你免费Spring Boot教程!

Spring Boot 获取 Bean 的 3 种方式!还有谁不会?相关推荐

  1. Spring读取配置文件,获取bean的几种方式

    Spring读取配置文件,获取bean的几种方式 方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemX ...

  2. SpringBoot静态获取 bean的三种方式,你学会了吗?

    欢迎关注方志朋的博客,回复"666"获面试宝典 来源:blog.csdn.net/showchi/article/details/97005720 注意:调用者要被spring管理 ...

  3. Spring注解创建Bean的几种方式

    Spring注解创建Bean的几种方式 1.@Component系列 @Component @Service @Repository @Controller @Configuration 2. 依附于 ...

  4. Spring Boot项目启动的几种方式

    Spring Boot项目启动的几种方式 方式一:右击启动或者点击intellij右上角的启动按钮 我们访问下浏览器看一下效果 方式二:利用maven启动 我们先进入到项目文件下,然后执行命令   m ...

  5. Spring获取ApplicationContext方式,和读取配置文件获取bean的几种方式

    Spring获取ApplicationContext方式 我自己常用的方法: 读取一个文件1 //创建Spring容器 2 ApplicationContext ctx = new ClassPath ...

  6. Spring Boot 实现定时任务的 4 种方式

    点击上方"方志朋",选择"设为星标" 做积极的人,而不是积极废人 作者:Wan QingHua wanqhblog.top/2018/02/01/SpringB ...

  7. Spring Boot 注册 Servlet 的3种方式

    一.Spring Boot 注册 Spring Boot 提供了 ServletRegistrationBean, FilterRegistrationBean, ServletListenerReg ...

  8. Spring实战(三)Spring中装配Bean的三种方式---XML、JavaConfig、AutoWire

    创建应用对象之间协作关系的行为称为装配(wiring),这也是依赖注入的本质. Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系,而开发者需要告诉Spring需要创建哪些 ...

  9. java websocket注解_【websocket】spring boot 集成 websocket 的四种方式

    集成 websocket 的四种方案 1. 原生注解 pom.xml org.springframework.boot spring-boot-starter-websocket WebSocketC ...

最新文章

  1. python跟java-Java与Python两大幸存者谁更胜一筹呢
  2. db:migrate
  3. 工业机器人打磨抛光编程员工资_让我们一起来谈谈,工业机器人行业的真实工资是多少?...
  4. (组合数学笔记)Pólya计数理论_Part.7_Pólya定理的母函数形式
  5. 如何 调系统相机_神仙理光相机,各种静物原片直出也太美了吧!!!
  6. maven没有servlet(创建servlet后报错)
  7. 剑指:合并两个排序的链表
  8. 微服务自动化部署(ansible playbook)干货之--zookeeper部署
  9. 2021年人工智能学习路线图分享
  10. 全国计算机联合考试广西二级c语言近五年试题,计算机二级c语言试题
  11. 小莫取色精灵 使用教程_MQ
  12. python 单例模式基本原则、使用场景、应用示例
  13. springboot中整合elasticsearch(基于springboot2.5.4,es版本7.13.2)
  14. linux 压缩 解压缩命令详解
  15. 嵌入式Linux学习经历 学前小结
  16. javamail模拟邮箱功能--邮件删除-中级实战篇【邮件标记方法】(javamail API电子邮件实例)
  17. 谷歌ai人工智能叫什么_谷歌正在通过AI策展和内置订阅全面革新Google新闻
  18. 基于 HTML5 WebGL 的 3D 水泥工厂生产线
  19. 演唱会系统mysql_演唱会售票管理系统数据库设计.doc
  20. 【招商银行数据方向笔试题】—— 信用卡推荐客户列表

热门文章

  1. EditText 中文文档
  2. SpringMVC 整合新浪微博登录 Java SDK
  3. Script:Speed Up Large Index Create or Rebuild
  4. golang go-sql-driver 数据库报错 bad connection
  5. json 数据类型简介
  6. docker 占用磁盘空间清理 无用数据卷删除
  7. Centos6.5下docker 环境搭建
  8. windows测试模式打开关闭
  9. java多线程 sleep()和wait()的区别
  10. Android--屏幕方向的改变