您的位置:首页 > 编程语言 > Java开发

由Spring管理的bean,不使用注入的方式来获取bean的方法

2019-04-27 22:11 169 查看

使用WebApplicationContextUtils来获取WebApplicationContext

WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
IUserServiceWs serviceWs = ctx.getBean("checkTokenService", IUserServiceWs.class);

通过ClassPathXmlApplicationContext加载applicationContext.xml文件来获取cxf

ClassPathXmlApplicationContext cxf = new ClassPathXmlApplicationContext("/applicationContext.xml");
IHelloService helloService = cxf.getBean("helloService",IHelloService.class);

通过Resource获取FactoryBean(XmlBeanFactory已过时)

//1.加载applicationContext.xml文件,从classpath路径找
Resource resource = new ClassPathResource("applicationContext.xml");
//2.创建并获取spring容器
BeanFactory beanFactory = new XmlBeanFactory(resource);
//3.得到bean对象
HelloWorld helloWorld = beanFactory.getBean("hello",HelloWorld.class);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐