我是靠谱客的博主 怡然睫毛膏,这篇文章主要介绍SpringBoot如何使用ApplicationContext获取bean对象,现在分享给大家,希望可以做个参考。

使用ApplicationContext获取bean对象

编写一个ApplicationContextFactory工厂类

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

在SpringBoot的启动类中设置ApplicationContext

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

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

复制代码
1
2
ApplicationContext applicationContext=ApplicationContextFactory.getApplicationContext(); Clazz clazz = applicationContext.getBean(Clazz.class);

SpringBoot Bean注入的深入研究

下面代码可正常运行

DemoService

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

CommonClass

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

Controller

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

下面代码不能正常运行

DemoService

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

CommonClass

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

Controller

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

比较

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

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

解决方案

新建工具类

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

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

DemoService

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

CommonClass

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

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

Controller

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

再次运行程序,一切正常

应用

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

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

最后

以上就是怡然睫毛膏最近收集整理的关于SpringBoot如何使用ApplicationContext获取bean对象的全部内容,更多相关SpringBoot如何使用ApplicationContext获取bean对象内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(482)

评论列表共有 0 条评论

立即
投稿
返回
顶部