spring boot 获取bean

在写测试用例的时候,如果是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;

猜你喜欢

转载自www.cnblogs.com/aspirant/p/10136103.html