在servlet的编程中,如果想用到Spring的注入功能,其实很简单:
看个例子程序:
package en.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import en.dao.UserDao;
public class UserLogin extends HttpServlet {
private UserDao dao;
public UserDao getDao() {
return dao;
}
public void setDao(UserDao dao) {
System.out.println("Injection seccess!");
this.dao = dao;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
String path="/userLogin.jsp";
//WebApplicationContext context = (WebApplicationContext)request.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
//dao = (UserDao)context.getBean("userDao");
dao.save();
request.getRequestDispatcher(path).forward(request, response);
}
}
可以发现,该servlet引用了一个持久层的类UserDao,而这个类,我们不想直接实例化,而是由Spring进行注入。那么问题是如何在servlet中调用Spring的框架实现该注入呢?当然一种比较笨拙的办法就是直接获取。也就是上述代码中被注释的那一段。然而很明显这不是最佳的解决方案,因为这样做就相当于将代码写死了。而实际上我们可以用一个代理的servlet来完成这一个功能。看代码:
package en.filter;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class ServletToBeanProxy extends HttpServlet {
private String targetBean;
private Servlet proxy;
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
proxy.service(req, res);
}
public void init()throws ServletException{
System.out.println("inti...");
targetBean = this.getInitParameter("targetBean");
System.out.println(targetBean);
getServletBean();
proxy.init(getServletConfig());
}
private void getServletBean() {
WebApplicationContext wac =
WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
this.proxy = (Servlet)wac.getBean(targetBean);
}
}
下面是该servlet的web.xml配置:
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ServletToBeanProxy</servlet-name>
<servlet-class>en.filter.ServletToBeanProxy</servlet-class>
<init-param>
<param-name>targetBean</param-name>
<param-value>userLoginServlet</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>UserLogin</servlet-name>
<url-pattern>/UserLogin</url-pattern>
</servlet-mapping>
首先,我们来理清一下代码的执行顺序。当我们在客户端输入网址:http://localhost:8080/ProjectName/UserLogin的时候,服务器就会去调用这个代理的servlet。然而这个代理的servlet到底做了什么呢?可以分几步来描述:一:获得init参数,这个参数就是实际要访问的servlet,他会在Spring的配置文件中定义。二:通过WebApplicationContext去Spring容器中得到目标servlet,当然名字就是init参数中配置的名字。三:将获得的servlet强制转换为Servlet,然后执行目标servlet。OK,这样就完成了注入。
那么,如果我们有另外一个业务的servlet怎么办呢?是不是还要在写一个ServletToBeanProxy 似地代理类呢?当然是不用的。我们只需要修改web.xml就行了。需要修改的地方只有init参数和url-parrten两处。代理类的代码是完全一样的,可以重用。这样,在servlet中就能感知到Spring的注入了。 而要注意的是,目标servlet是不需要在web.xml当中配置的。
分享到:
相关推荐
需要在 web.xml 中注册 DispatcherServlet <servlet> <servlet-name>dispatherContext</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> ...
Spring MVC是Spring框架中用于Web应用快速开发的一个模块,是当今流行的Web开发框架之一。 本书是Servlet、JSP和Spring MVC的学习指南。全书内容分为两个部分,首部分主要介绍Servlet和JSP基础知识和技术,包括第1...
java servlet spring web filter listenr
SpringBoot配置嵌入式Servlet容器;注册Servlet三大组件;嵌入式Servlet容器自动配置原理以及启动原理
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1 </servlet> <servlet-mapping> <servlet-name>test</servlet-name> *.do </servlet-mapping> ...
SpringSecurity,这是一种基于Spring AOP和Servlet过滤器的安全框架。...本实例简单配置了springsecurity权限控制,提供admin,user两个用户,在页面登录时以不同角色可访问不同页面,初学者可加以参考此例 谢谢!
在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: ...
spring mvc3.2.7(基于注解配置)+servlet3.1(jetty)+maven3+eclipse
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0"> <property name="interceptors"> <list> <ref bean="doubleSubmitInterceptor"/> </list> ...
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
采用注解配置SpringMVC,有SpringSecurity,Dao,Controller,Servlet等的样例,部分代码: public void onStartup(ServletContext servletContext) throws ServletException { //日志文件配置 servletContext....
配置 SimpleUrlHandlerMapping, 在上面的配置文件中, /hello.do 的请求将被 helloController 处理. "/hello.do"和"helloController" 是变量, 你可以更改. 但是你注意到了吗, hello.do 以 .do 作为后缀名. 如果这里...
spring mvc maven项目,导入IDEA后无报错,需要在IDEA中配置Tomcat并将项目添加到tomcat才能运行。 可用于分析spring mvc源码、spring mvc父子容器初始化流程、session和cookie机制、spring session等,也可以用于...
jsp+servlet+spring+hibernate架构在myeclipse中的配置步骤
SpringBoot 注册自己的Servlet(三种方式)(源代码) ...目前使用Spring的,更多是在web方面,所以Spring更多的是依赖Servlet容器,哪怕是springboot擅长做独立可执行的微服务应用程序,其内部也包含一个嵌入式Jetty。
Spring mvc 配置: WEB/INF/springmvc-servlet.xml//编码过滤器//静态资源忽略//AOP代理方式设定//拦截器配置//cglib动态代理
sp页面的form表单里面有一个属性值action,当提交jsp页面上的form表单时,系统会根据action的值来找到servlet里面具体的action,(这里的servlet是在web.xml配置文件里面),然后调用action里面的execute()方法,...
web.xml 中定义了一个名为 annomvc 的 Spring MVC 模块,按照 Spring MVC 的契约,需要在 WEB-INF/annomvc-servlet.xml 配置文件中定义 Spring MVC 模块的具体配置。annomvc-servlet.xml 的配置内容如下所示: ...
把要用blazeds公开给flex的java方法配置在flex-servlet.xml中 把spring整合其它框架的方法的配置放在spring文件夹中。 很有条理 用remoteObject做的helloWorld。 适合新手入门和老手改用spring3来做项目。 如要交流...
8、spring配置文件中namespace的事情,不感兴趣 9、非标准setter类也可以注入了。。。不感兴趣 10、支持Servlet3的某个东东,可以写程序直接启动webapp,而非web.xml,不感兴趣 11、支持servlet3的上传东东,可能是...