zl程序教程

您现在的位置是:首页 >  后端

当前栏目

spring boot 获取bean

SpringBoot 获取 bean
2023-09-14 09:02:31 时间
在写测试用例的时候,如果是springboot的应该加上 springboot的标签:
@SpringBootTest(classes = ApplicationLoader.class)
@ActiveProfiles("dev")
@RunWith(SpringJUnit4ClassRunner.class)

 

调用某个Listener的两个方法:
一、
SpringUtil.getBean(AppealDetourListener.class);
就是:
(1)applicationContext.getBean(AppealDetourListener.class);
(2)applicationContext.getBean("detourDataListener");
都可以
但是不能用下面的,因为这是springMVC的标签:
(AppealDetourListener) ContextLoader.getCurrentWebApplicationContext().getBean("detourDataListener");

二、用自动注入的方式也可以;

 @Autowired
    private AppealDetourListener appealDetourListener;