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;

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM