经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
SpringBoot如何使用ApplicationContext获取bean对象
来源:jb51  时间:2021/11/16 15:13:46  对本文有异议

使用ApplicationContext获取bean对象

编写一个ApplicationContextFactory工厂类

  1. public class ApplicationContextFactory{
  2. private static ApplicationContext applicationContext = null;
  3. public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  4. applicationContext = applicationContext;
  5. }
  6. public static ApplicationContext getApplicationContext(){
  7. return applicationContext;
  8. }
  9. }

在SpringBoot的启动类中设置ApplicationContext

  1. public class Application {
  2. public static void main(String[] args) {
  3. ConfigurableApplicationContext app = SpringApplication.run(Application.class, args);
  4. ApplicationContextFactory.setApplicationContext(app);
  5. }
  6. }

通过ApplicationContextFactory获取SpringApplication从而获取bean对象

  1. ApplicationContext applicationContext=ApplicationContextFactory.getApplicationContext();
  2. Clazz clazz = applicationContext.getBean(Clazz.class);

SpringBoot Bean注入的深入研究

下面代码可正常运行

DemoService

  1. @Service
  2. public class DemoService {
  3. public void save(){
  4. System.out.println("DemoService save");
  5. }
  6. }

CommonClass

  1. @Component
  2. public class CommonClass {
  3. @Resource
  4. private DemoService demoService;
  5. public void fun(){
  6. System.out.println("fun");
  7. demoService.save();
  8. }
  9. }

Controller

  1. @Resource
  2. private CommonClass commonClass;
  3. @ResponseBody
  4. @GetMapping("/fun")
  5. public void fun(){
  6. commonClass.fun();
  7. }

下面代码不能正常运行

DemoService

  1. @Service
  2. public class DemoService {
  3. public void save(){
  4. System.out.println("DemoService save");
  5. }
  6. }

CommonClass

  1. public class CommonClass {
  2. @Resource
  3. private DemoService demoService;
  4. public void fun(){
  5. System.out.println("fun");
  6. demoService.save();
  7. }
  8. }

Controller

  1. @ResponseBody
  2. @GetMapping("/fun")
  3. public void fun(){
  4. CommonClass commonClass = new CommonClass();
  5. commonClass.fun();
  6. }

比较

比较两个代码发现后者与前者的区别:因后者的CommonClass 没有使用@Component标注,所以在Controller中不能才用注入方式生成CommonClass对象,而是才用new的方式生成了该对象。

这样一来,CommonClass 对象是手工创建,所以在它内部注入DemoService 对象的代码就错误了。

解决方案

新建工具类

  1. @Component
  2. public class ApplicationContextUtil implements ApplicationContextAware {
  3. private static ApplicationContext act;
  4. @Override
  5. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  6. act = applicationContext;
  7. }
  8. /**
  9. * 根据bean的名字获取工厂中对应的bean对象
  10. * @param beanName
  11. * @return
  12. */
  13. public static Object getBean(String beanName){
  14. return act.getBean(beanName);
  15. }
  16. }

:实际测试发现上面代码中的static不能省略

DemoService

  1. @Service
  2. public class DemoService {
  3. public void save(){
  4. System.out.println("DemoService save");
  5. }
  6. }

CommonClass

  1. public class CommonClass {
  2. @Resource
  3. private DemoService demoService;
  4. public void fun(){
  5. DemoService demoService = (DemoService) ApplicationContextUtil.getBean("demoService");
  6. System.out.println("fun");
  7. demoService.save();
  8. }
  9. }

此处不再采用注入的方式获取DemoService对象,而是通过工具类的方式

Controller

  1. @ResponseBody
  2. @GetMapping("/fun")
  3. public void fun(){
  4. CommonClass commonClass = new CommonClass();
  5. commonClass.fun();
  6. }

再次运行程序,一切正常

应用

在SpringBoot整合Shiro的案例中,自定义Realm时,需要使用Service的对象。因为自定义的Realm类不能使用@Component之类的注解注释,所以使用本案例介绍的方法是正确的解决方案。尽管在1.6.0的shiro-all中下面代码可以正确运行:

在这里插入图片描述

以上为个人经验,希望能给大家一个参考,也希望大家多多支持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号