由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);
相关文章推荐
- 将spring管理的bean使用注解的方式注入到servlet中
- spring bean 不使用注入的方式获取的两种方式
- HttpServlet注入Spring管理的bean的方法
- spring 配置bean的方法及依赖注入发方式
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况)
- spring-01 spring的bean管理(使用配置文件方式创建对象 附代码)
- Spring 无法注入静态变量,直接在静态方法中获取bean
- spring4笔记----使用装配注入合作者Bean的三种方式
- 使用Spring方法注入协调不同步的bean
- Spring中事务管理支持哪几种方式以及每种方式的具体使用方法
- 使用Spring(八) 延迟初始化bean、Lookup方法注入
- spring+springMVC中使用@Transcational方式管理事务的配置方法
- spring-02 spring的bean管理(使用注解方式创建对象 附代码)
- 关于使用spring获取bean的方法
- spring使用注解方式该如何在框架外获取注入?
- 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法
- spring中使用@RequestBody,对应的bean中属性名大写注入为空的解决方法
- 【spring set注入 注入集合】 使用set注入的方式注入List集合和Map集合/将一个bean注入另一个Bean
- Spring管理的bean初始化方法的三种方式,以及@PostConstruct不起作用的原因
- Java获取Tomcat启动中的spring管理的容器注入的实体,用来获取相应的bean