Spring获取ApplicationContext对象工具类的实现方法
Spring获取ApplicationContext对象工具类的实现方法
(1)实现的工具类:
packagecom.util; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; finalpublicclassApplicationContextUtil{ privatestaticApplicationContextac=null; privateApplicationContextUtil(){ } static{ ac=newClassPathXmlApplicationContext("applicationContext.xml"); } publicstaticApplicationContextgetApplicationContext(){ //获得返回的容器对象 returnac; } }
(2)使用方法:
publicstaticvoidgetByUtil(){ ApplicationContextUtil.getApplicationContext().getBean("userService"); }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!