- 浏览: 3443442 次
- 性别:
- 来自: China
-
文章分类
- 全部博客 (536)
- ajax (1)
- Algorithm (14)
- Android (40)
- CSS/HTML... (2)
- defy (3)
- DesignPattern (2)
- dorado (0)
- Drools (6)
- English/日本語 (7)
- Flex (2)
- Framework (0)
- Google (3)
- hibernate (13)
- homework (3)
- HTML5 (0)
- IDE (29)
- java (45)
- javaee (7)
- Javascript (14)
- java组件 (5)
- jQuery (4)
- jsp (8)
- jsf (2)
- Linux (2)
- lucene (0)
- mysql (6)
- news (3)
- Oracle (8)
- other (4)
- PHP (5)
- Python (0)
- Software Engineering (3)
- spring (7)
- struts1.x (14)
- struts2.x (14)
- strolling in cloud (1)
- subject:javaEnhance (20)
- Tomcat (7)
- validator (3)
- 学习·方法·心得 (8)
- .NET (2)
- vba (6)
- groovy (5)
- grails (2)
- SWT (0)
- big data (1)
- perl (1)
- objective-c (50)
- product (1)
- mac (7)
- ios (188)
- ios-phone (2)
- ios-system (15)
- ios-network (5)
- ios-file (4)
- ios-db (1)
- ios-media (3)
- ios-ui (27)
- ios-openSource (6)
- ios-animation (5)
- ios-drawing (7)
- c (2)
- ios-app (2)
- ios-course (15)
- ios-runtime (14)
- ios-code (8)
- ios-thread (8)
- ios-LBS (2)
- ios-issue (1)
- ios-design (2)
- Jailbreak (2)
- cocos2d (0)
- swift (16)
- ios-framework (4)
- apple watch (4)
- ios-web (1)
- react native (3)
- TVOS (1)
- OpenGL (1)
最新评论
-
xiaobinggg:
...
Session机制详解 -
菜鸟学生会:
Drools规则工作流引擎开发教程网盘地址:http://pa ...
Drools入门-----------环境搭建,分析Helloworld -
wangyudong:
不是很好用,不支持自动化测试RESTful API,也不支持自 ...
Simple REST Client POST使用方法 -
Paul0523:
很棒的一篇文章,感谢楼主分享
Session机制详解 -
啸笑天:
获取原型对象的三种方法<script>functi ...
复习JavaScript面向对象技术
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
<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,配置上更简单。
备注:红色即为要和applicationContext.xml中为filter和servlet配置的bean的id相同
发表评论
-
Session机制详解
2012-06-29 00:35 183473虽然session机制在web应用 ... -
spring事务(附件pdf)
2011-12-08 21:27 1657事务传播属性 REQUIRED ... -
java模拟spring ioc
2011-12-08 17:21 2218<?xml version="1.0&q ... -
Spring aop 简单总结
2011-12-08 16:59 2533先用jdk,cglib模拟下: 使用JDK动态代理 //当 ... -
Spring 积累
2011-12-08 17:23 1873三种实例化bean的方式 1.使用类构造器实例化 & ... -
Drools与Spring集成 登录测试
2011-11-26 23:09 21797Drools5.2.0.Final与Spring3集成测试 ... -
javaweb读取任意目录的下的properties配置文件(解决普通java类读web-inf下任意目录)
2011-10-28 15:59 11896看到很多用getResourcesAsStream()来读取. ... -
Spring 积累
2011-05-29 11:08 61 -
搜索搜索
2011-02-19 21:34 01102194672616 -
Session,Cookie,jsessionid,Url重写
2010-12-02 16:22 19976在一些投票之类的场合,我们往往因为公平的原则要求每人只能投一票 ... -
Spring中与Bean相关的接口
2010-11-17 23:27 2835package chapter22; impor ... -
J2EE应用的五层架构
2010-10-23 22:01 3370如图: -
Servlet和filter的JAVAEE的配置
2010-05-27 18:00 2882一、servlet容器对url的匹配过程: 当一个请求 ... -
PO/POJO/BO/DTO/VO/DAO的区别
2010-05-26 22:20 3803感谢http://holdbelief.iteye.c ... -
java乱码
2010-05-22 02:57 1985二、中文字符乱码的原因及解决办法 java的内核是Unic ... -
今天做自定义标签时才发现:EVAL_PAPGE、SKIP_PAGE-----我这个菜鸟啊
2010-05-15 20:54 8515继承关系 java.lang.Object ... -
pageContext.findAttribute()与pageContext.getAttribute()的区别
2010-05-07 22:24 52211、abstract Object findAttribut ... -
我用servlet读取数据库图片文件已经成功
2010-03-19 11:22 3342我用servlet读取数据库图片文件已经成功 可以单独显示 现 ... -
MySQL和JDBC中文乱码问题
2009-12-18 19:50 4137MySQL和JDBC的中文乱码问题一直是比较麻烦,首先在考虑 ... -
JSP 防止重复提交 防止重复刷新 防止后退问题以及处理方式
2009-12-18 19:44 2240一。前言你在任何一 ...
相关推荐
Spring 管理filter 和servlet
基于Spring和Mybatis和servlet的人员信息管理系统源码+数据库.zip基于Spring和Mybatis和servlet的人员信息管理系统源码+数据库.zip基于Spring和Mybatis和servlet的人员信息管理系统源码+数据库.zip基于Spring和...
Spring的注入在Servlet中使用:在Servlet中使用Spring注入的信息,需要WebApplicationContext这个专门为Web准备的应用上下文
这个是搭建与测试Spring的开发环境和编码剖析Spring 管理Bean的原理的代码,适合刚接触spring的人学习,如何搭建spring开发环境,以及了解spring是如何管理bean的。这个是由传智播客的老师讲的。
spring自动扫描和管理Bean的示例
基于Spring和Mybatis和servlet的人员信息管理系统
spring动态向容器中添加bean和删除指定bean,不需要重启应用
Spring bean 管理大全 全面的介绍springbean各种操作原理
Spring和Servlet的整合应用,在Spring的容器中添加Servlet类的应用。实现spring框架和Servlet的联合应用。
初始化后可访问Spring管理的Bean
spring bean配置 运行环境:eclipse 构建工具:maven 不提供maven构建,maven用来解决jar包的依赖
spring 与 servlet整合
spring 与 jquery ajax 交互 和 servlet 与jquery ajax交互
Servlet JSP和Spring MVC初学指南
Spring Bean创建初始化流程
bean加入spring容器管理的方式,bean加入applicationcontext容器的方式
spring运行过程中动态注册bean,代码实例
spring bean 的生命周期,把运行结果的日志,用sublime打开对比查看,你会有比较清晰的认识