web读取spring管理的bean,基于xml
2013-04-01 23:33
218 查看
初识spring框架的时候,可能对于bean的管理,也就是IOC容器的使用,也许不是很困难的事情。
然而,我们开发过程中,往往都是将spring进行javaee的开发,这对于web开发中也许就会带来问题了,虽然所需要的bean已经实例化了在IOC容器当中,然而我们在自己的业务程序中怎么获取到自己想要的bean确是一件比较头痛的事情。
这里主要介绍两种常用的方式:
1、实现spring的ApplicationContextAware接口
首先在web.xml中配置(这里加入了struts2作为控制器):
定义工具类实现ApplicationContextAware,这样在程序中就可以通过工具类获取bean
编写applicationContext.xml
定义Action类:
继续列举struts.xml的配置:
测试后台输出:
service9
9
第二种方法,其实只是BeanProvider 发生了变化,通过BeanFactory接口获得Bean,实现的是BeanFactoryAware 接口
还需要在applicationContext.xml文件中修改:
对于UserAction的代码,修改如下:
测试后台结果:
service9
9
通过以上两种方法都可以在web程序中获取SpringIOC当中的bean。
此外,其实还有一种方式,可以通过WebApplicationContext 获取bean,不过这种方法的性能较差,这边就不列举了。不建议使用这种方式。
然而,我们开发过程中,往往都是将spring进行javaee的开发,这对于web开发中也许就会带来问题了,虽然所需要的bean已经实例化了在IOC容器当中,然而我们在自己的业务程序中怎么获取到自己想要的bean确是一件比较头痛的事情。
这里主要介绍两种常用的方式:
1、实现spring的ApplicationContextAware接口
首先在web.xml中配置(这里加入了struts2作为控制器):
<!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring-Context loader --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:application*.xml</param-value> </context-param> <!--Spring ApplicationContext 载入 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>定义需要管理的类:
package org.enson.service; public class UserService { public int add(int a , int b){ System.out.println("service"+(a+b)); return a+b; } }
定义工具类实现ApplicationContextAware,这样在程序中就可以通过工具类获取bean
package org.common; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; //此类必须配置在spring的xml文件中 public class BeanProvider implements ApplicationContextAware { private static ApplicationContext applicationContext = null ; //由spring容器自动实例化ApplicationContext public void setApplicationContext(ApplicationContext context) throws BeansException { BeanProvider.applicationContext = context ; } //通过名称获取bean public static Object getBeans(String beanName){ return applicationContext.getBean(beanName); } }
编写applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="beanProvider" class="org.common.BeanProvider"/> <bean id="userService" class="org.enson.service.UserService"/> </beans>
定义Action类:
package org.enson.action; import org.common.BeanProvider; import org.enson.service.UserService; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { private static final long serialVersionUID = 1L; private UserService userService = (UserService) BeanProvider .getBeans("userService");//这边可以直接从容器中拿到对象 public String test() { System.out.println(userService.add(3, 6)); return SUCCESS; } }
继续列举struts.xml的配置:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="test" extends="struts-default" namespace="/"> <default-action-ref name="user"></default-action-ref> <action name="user" class="org.enson.action.UserAction" method="test"> <result name="success">index.jsp</result> </action> </package> </struts>
测试后台输出:
service9
9
第二种方法,其实只是BeanProvider 发生了变化,通过BeanFactory接口获得Bean,实现的是BeanFactoryAware 接口
package org.common; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; public class BeanFactoryProvidor implements BeanFactoryAware { private static BeanFactory beanFactory = null; public void setBeanFactory(BeanFactory beanFactory) throws BeansException { BeanFactoryProvidor.beanFactory = beanFactory; } public static Object getBeans(String beanName){ return beanFactory.getBean(beanName); } }
还需要在applicationContext.xml文件中修改:
<bean id="beanFactoryProvidor" class="org.common.BeanFactoryProvidor"/> <bean id="userService" class="org.enson.service.UserService"/>
对于UserAction的代码,修改如下:
private UserService userService = (UserService) BeanFactoryProvidor .getBeans("userService"); public String test() { System.out.println(userService.add(3, 6)); return SUCCESS; }
测试后台结果:
service9
9
通过以上两种方法都可以在web程序中获取SpringIOC当中的bean。
此外,其实还有一种方式,可以通过WebApplicationContext 获取bean,不过这种方法的性能较差,这边就不列举了。不建议使用这种方式。
相关文章推荐
- spring中事务的管理基于xml和注解,以及spring在web中应用
- 【JavaWeb-23】spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- Spring学习历程---Bean基于xml的装配
- 《step4:tiny-spring-ioc学习四》——读取xml配置来初始化bean
- spring中读取xml配置文件、获取bean的几种方式
- spring-配置bean(3、基于xml配置)
- Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析
- 基于注解的Spring MVC(所需jar包,web.xml配置,Spring文件配置,@Controller,@RequestMapping,@RequestParam,model填参,EL取值)
- Spring整合quartz配置【一】基于MethodInvokingJobDetailFactoryBean实现任务管理
- ssh项目中,对于spring的bean.xml和web.xml文件的基本配置
- SpringBoot Junit 测试加载 xml 读取 bean 失败
- spring中读取xml配置文件、获取bean的几种方式
- SPRINGMYBATIS01 Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用
- Spring--IoC--基于XML的DI-异类抽象Bean
- Spring中 基于注解的装配Bean 与 基于xml的对比总结
- web.xml之Spring配置(基于SSI)
- spring基于xml加载和读取properties配置文件的方式
- Spring IOC使用(3) 基于注解管理bean
- spring的Bean管理(xml)
- spring基础-Bean的装配方式(一)基于xml的装配