Spring管理filter和servlet,无硬编码bean
2014-02-24 16:20
357 查看
Spring管理filter和servlet,无需硬编码硬编码bean
在完成的项目中,要为别的项目提供接口,可选方案:webservice和post请求。现用post请求。
在使用spring容器的web应用中,业务对象间的依赖关系都可以用applicationContext.xml文件来配置,并且由spring容器来负责依赖对象的创建。如果要在filter或者servlet中使用spring容器管理业务对象,通常需要使用WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext())来获得WebApplicationContext,然后调用WebApplicationContext.getBean("beanName")来获得对象的引用,这实际上是使用了依赖查找来获得对象,并且在filter或者servlet代码中硬编码了应用对象的bean名字。
为了能在filter或者servlet中感知spring中bean,可采用如下步骤来实现:
1-将filter或者servlet作为bean定义在spring配置文件applicationContext.xml中,和其他应用的bean定义放在一起;
2-实现一个filter代理或者servlet代理,该代理用WebApplicationContext来获得在applicationContext.xml中定义的filter或者servlet的对象,并将任务委托给applicationContext.xml中定义的filter或者servlet;
3-在web.xml中用ContextLoaderListener来初始化spring的context。
4-在web.xml中,同时在filter代理或者servlet代理的定义中用初始化参数来定义applicationContext.xml中filter或者servlet的bean名字(或者直接受用代理的名称获得相应的filter或者servlet的名称)。定义filter代理或者servlet代理的mapping。
利用这种方式就将filter或者servlet和业务对象的依赖关系用spring来进行管理,并且不用在servlet中硬编码要引用的对象名字。
具体实例如下:
Filter
1.在applicationContext.xml中定义filter
说明:com.netqin.filter.SpringFilter为实现了javax.servlet.Filter接口的filter
2.实现filter代理
实际上,filter代理不需要我们自己来实现,Spring提供了两种现成的filter代理org.springframework.security.util.FilterToBeanProxy,org.springframework.web.filter.DelegatingFilterProxy,两者只是在web.xml中的配置上略有不同,下面就让我们一起看看如何在web.xml中进行配置。
3.配置web.xml
初始化spring的context
因为是使用spring来管理,所以在使用filter前先要初始化spring的context,一般来说配置如下:
4,Filter配置:
FilterToBeanProxy代理的配置
说明:需要为FilterToBeanProxy提供上下文参数,这里我们配置的是targetBean属性,它告诉spring在context中查找的bean名称,所以当请求被过滤器拦截后FilterToBeanProxy会在applicationContext.xml中会查找id为springFilter的bean.
我们也可以配置targetClass属性,意思就是查找该类型的bean.
DelegatingFilterProxy代理的配置
说明:使用DelegatingFilterProxy时不需要配置任何参数,spring会根据filter-name的名字来查找bean,所以这里spring会查找id为springFilter的bean.
OK!filter配置完成。推荐使用DelegatingFilterProxy,配置上更简单。
Servlet
Servlet的配置与Filter的配置十分相似
1.在applicationContext.xml中定义servlet
说明:com.jsptpd.jczc.temp.GongGuanTest继承自javax.servlet.http.HttpServlet,如下:
2.实现servlet代理,创建一个servlet代理十分简单,一个具体的实现如下:
说明:相信看了代码就明白了,它利用targetBean属性在spring中查找相应的servlet,这很像FilterToBeanProxy的方式,所以我为其取名为ServletToBeanProxy。当然,我们也可以使用类似于DelegatingFilterProxy的方式,只需要将上述代码中this.targetBean = getInitParameter("targetBean");修改为this.targetBean =this.getServletName();即可,我们相应的命名为DelegatingServletProxy。
3.配置web.xml
初始化spring的context,与filter中的说明一致。
4.Servlet配置:
ServletToBeanProxy代理的配置
DelegatingServletProxy代理的配置
OK!servlet的配置完成。推荐使用DelegatingServletProxy,配置上更简单。
在完成的项目中,要为别的项目提供接口,可选方案:webservice和post请求。现用post请求。
在使用spring容器的web应用中,业务对象间的依赖关系都可以用applicationContext.xml文件来配置,并且由spring容器来负责依赖对象的创建。如果要在filter或者servlet中使用spring容器管理业务对象,通常需要使用WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext())来获得WebApplicationContext,然后调用WebApplicationContext.getBean("beanName")来获得对象的引用,这实际上是使用了依赖查找来获得对象,并且在filter或者servlet代码中硬编码了应用对象的bean名字。
为了能在filter或者servlet中感知spring中bean,可采用如下步骤来实现:
1-将filter或者servlet作为bean定义在spring配置文件applicationContext.xml中,和其他应用的bean定义放在一起;
2-实现一个filter代理或者servlet代理,该代理用WebApplicationContext来获得在applicationContext.xml中定义的filter或者servlet的对象,并将任务委托给applicationContext.xml中定义的filter或者servlet;
3-在web.xml中用ContextLoaderListener来初始化spring的context。
4-在web.xml中,同时在filter代理或者servlet代理的定义中用初始化参数来定义applicationContext.xml中filter或者servlet的bean名字(或者直接受用代理的名称获得相应的filter或者servlet的名称)。定义filter代理或者servlet代理的mapping。
利用这种方式就将filter或者servlet和业务对象的依赖关系用spring来进行管理,并且不用在servlet中硬编码要引用的对象名字。
具体实例如下:
Filter
1.在applicationContext.xml中定义filter
<bean id="springFilter" class="com.netqin.filter.SpringFilter"> <property name="name"> <value>zyj</value> </property> </bean>
说明:com.netqin.filter.SpringFilter为实现了javax.servlet.Filter接口的filter
2.实现filter代理
实际上,filter代理不需要我们自己来实现,Spring提供了两种现成的filter代理org.springframework.security.util.FilterToBeanProxy,org.springframework.web.filter.DelegatingFilterProxy,两者只是在web.xml中的配置上略有不同,下面就让我们一起看看如何在web.xml中进行配置。
3.配置web.xml
初始化spring的context
因为是使用spring来管理,所以在使用filter前先要初始化spring的context,一般来说配置如下:
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring-configs/base/applicationContext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
4,Filter配置:
FilterToBeanProxy代理的配置
<filter> <filter-name> springFilter </filter-name> <filter-class> org.springframework.security.util.FilterToBeanProxy </filter-class> <init-param> <param-name>targetBean</param-name> <param-value>springFilter</param-value> </init-param> </filter> <filter-mapping> <filter-name> springFilter </filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
说明:需要为FilterToBeanProxy提供上下文参数,这里我们配置的是targetBean属性,它告诉spring在context中查找的bean名称,所以当请求被过滤器拦截后FilterToBeanProxy会在applicationContext.xml中会查找id为springFilter的bean.
我们也可以配置targetClass属性,意思就是查找该类型的bean.
DelegatingFilterProxy代理的配置
<filter> <filter-name>springFilter</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> </filter> <filter-mapping> <filter-name> springFilter </filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
说明:使用DelegatingFilterProxy时不需要配置任何参数,spring会根据filter-name的名字来查找bean,所以这里spring会查找id为springFilter的bean.
OK!filter配置完成。推荐使用DelegatingFilterProxy,配置上更简单。
Servlet
Servlet的配置与Filter的配置十分相似
1.在applicationContext.xml中定义servlet
<bean id="springServlet" class="com.jsptpd.jczc.temp.GongGuanTest"> <property name="name" value="zyj"/> <property name="siteSpecialTestDao" ref="jczc.siteSpecialTestDao"/> </bean>
说明:com.jsptpd.jczc.temp.GongGuanTest继承自javax.servlet.http.HttpServlet,如下:
package com.jsptpd.jczc.temp; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jsptpd.jczc.signal.dao.impl.SiteSpecialTestDaoImpl; public class GongGuanTest extends HttpServlet { private static final long serialVersionUID = 1L; private String name; private SiteSpecialTestDaoImpl siteSpecialTestDao; public SiteSpecialTestDaoImpl getSiteSpecialTestDao() { return siteSpecialTestDao; } public void setSiteSpecialTestDao(SiteSpecialTestDaoImpl siteSpecialTestDao) { this.siteSpecialTestDao = siteSpecialTestDao; } public String getName() { return name; } public void setName(String name) { this.name = name; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=utf-8"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); request.setCharacterEncoding("utf-8"); out.println(name.toString() + "<br>"); out.println(siteSpecialTestDao.toString() + "<br>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }
2.实现servlet代理,创建一个servlet代理十分简单,一个具体的实现如下:
package com.jsptpd.jczc.temp.proxy; import java.io.IOException; import javax.servlet.GenericServlet; import javax.servlet.Servlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; public class ServletToBeanProxy extends GenericServlet { private String targetBean; private Servlet proxy; public void init() throws ServletException { this.targetBean = getInitParameter("targetBean"); getServletBean(); proxy.init(getServletConfig()); } public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { proxy.service(req, res); } private void getServletBean() { //实现一个servlet代理,该代理用WebApplicationContext来获得在applicationContext.xml中定义的servlet的对象,并将任务委托给applicationContext.xml中定义的servlet; WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); this.proxy = (Servlet) wac.getBean(targetBean); } }
说明:相信看了代码就明白了,它利用targetBean属性在spring中查找相应的servlet,这很像FilterToBeanProxy的方式,所以我为其取名为ServletToBeanProxy。当然,我们也可以使用类似于DelegatingFilterProxy的方式,只需要将上述代码中this.targetBean = getInitParameter("targetBean");修改为this.targetBean =this.getServletName();即可,我们相应的命名为DelegatingServletProxy。
3.配置web.xml
初始化spring的context,与filter中的说明一致。
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring-configs/base/applicationContext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
4.Servlet配置:
ServletToBeanProxy代理的配置
<servlet> <servlet-name>springServlet</servlet-name> <servlet-class> com.jsptpd.jczc.temp.proxy.ServletToBeanProxy </servlet-class> <init-param> <param-name>targetBean</param-name> <param-value>springServlet</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name> springServlet </servlet-name> <url-pattern>/gongguantest.htc</url-pattern> </servlet-mapping>
DelegatingServletProxy代理的配置
<servlet> <servlet-name>springServlet</servlet-name> <servlet-class> com.jsptpd.jczc.temp.proxy.DelegatingServletProxy </servlet-class> </servlet> <servlet-mapping> <servlet-name> springServlet </servlet-name> <url-pattern>/gongguantest.htc</url-pattern> </servlet-mapping>
OK!servlet的配置完成。推荐使用DelegatingServletProxy,配置上更简单。
相关文章推荐
- Spring管理filter和servlet ( FilterToBeanProxy/DelegatingFilterProxy)
- servlet 使用spring管理bean
- Spring管理filter和servlet,无硬编码bean
- 在servlet中或者在filter中获取spring容器中的bean
- Spring管理Filter和Servlet
- Servlet中调用Spring管理的Bean
- Servlet中获取spring bean的管理对象
- Spring管理filter和servlet
- Spring管理filter和servlet
- Servlet中访问spring容器管理的bean
- Spring管理filter和servlet
- Spring管理filter和servlet
- servlet 中获取 spring 管理的 bean
- 在servlet中获取spring管理的bean
- Spring管理filter和servlet
- servlet中注入spring管理的bean
- 非spring组件servlet、filter、interceptor中注入spring bean
- Spring管理Filter和Servlet
- Spring管理filter和servlet
- Spring管理filter和servlet