经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
SpringBoot项目如何将Bean注入到普通类中
来源:jb51  时间:2021/11/15 20:02:39  对本文有异议

如何将Bean注入到普通类中

Spring管理的类获得一个注入的Bean方式

@Autowired是一种注解,可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作,自动执行当前方法,如果方法有参数,会在IOC容器中自动寻找同类型参数为其传值。

如Controller中注入Bean可以这么写:

  1. @RestController
  2. public class TestController {
  3. @Autowired
  4. public TestBean bean;// 配置文件bean
  5. }

非Spring管理的类获得一个注入的Bean方式

上述通过@Autowired注解注入的方式只可以用在Spring管理的类中,而普通类中通过这样的方式获得的Bean为null。

可以使用Spring上下文ApplicationContext获得Bean的方式,将Bean注入到普通类中

普通类中通过ApplicationContext上下文获得Bean

  1. public class Test{
  2. //声明要注入的Bean变量
  3. private static TestBean bean;
  4. // 通过applicationContext上下文获取Bean
  5. public static void setApplicationContext(ApplicationContext applicationContext) {
  6. bean = applicationContext.getBean(TestBean.class);
  7. }
  8. }

修改SpringBoot启动类

将ApplicationContext传入普通类中

  1. @SpringBootApplication
  2. public class TestrApplication {
  3. public static void main(String[] args) {
  4. final ApplicationContext applicationContext = SpringApplication.run(TestApplication.class, args);
  5. // 将上下文传入Test类中,用于检测tcp连接是否正常
  6. Test.setApplicationContext(applicationContext);
  7. }
  8. }

这样一个Spring的Bean就可以注入到普通类中使用了.

在普通类中如何获取Bean节点

  1. /*
  2. * 文件名:SpringContextUtil.java
  3. * 描述:获取bean实例工具类
  4. * 修改人:Wang Chinda
  5. * 修改时间:2018/10/30
  6. * 修改内容:新增
  7. */
  8. package com.chinda.utils;
  9. import org.springframework.beans.BeansException;
  10. import org.springframework.context.ApplicationContext;
  11. import org.springframework.context.ApplicationContextAware;
  12. import org.springframework.stereotype.Component;
  13. /**
  14. * 获取bean实例工具类
  15. * @author Wang Chinda
  16. * @date 2018/10/30
  17. * @see
  18. * @since 1.0
  19. */
  20. @Component
  21. public class SpringContextUtil implements ApplicationContextAware {
  22. /**
  23. * 上下文对象实例
  24. */
  25. private static ApplicationContext applicationContext;
  26. @Override
  27. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  28. this.applicationContext = applicationContext;
  29. }
  30. /**
  31. * 获取applicationContext
  32. *
  33. * @return
  34. */
  35. public static ApplicationContext getApplicationContext() {
  36. return applicationContext;
  37. }
  38. /**
  39. * 通过name获取Bean.
  40. *
  41. * @param name
  42. * @return
  43. */
  44. public static Object getBean(String name) {
  45. return getApplicationContext().getBean(name);
  46. }
  47. /**
  48. * 通过class获取Bean.
  49. *
  50. * @param clazz
  51. * @param <T>
  52. * @return
  53. */
  54. public static <T> T getBean(Class<T> clazz) {
  55. return getApplicationContext().getBean(clazz);
  56. }
  57. /**
  58. * 通过name以及Class返回指定的Bean
  59. *
  60. * @param name
  61. * @param clazz
  62. * @param <T>
  63. * @return
  64. */
  65. public static <T> T getBean(String name, Class<T> clazz) {
  66. return getApplicationContext().getBean(name, clazz);
  67. }
  68. }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号