在应用中一般普通的Java Pojo都是由Spring来管理的,所以使用autowire注解来进行注入不会产生问题,但是有两个东西是例外的,一个是Filter,一个是Servlet,这两样东西都是由Servlet容器来维护管理的,所以若想和其他的Bean一样使用Autowire来注入的话,是需要做一些额外的功夫的。
第1步:在web.xml中注册Spring的监听器
<!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
第2步:编写一个管理Servlet的类
这个类主要是将Servlet和Spring中的Bean结合起来,方便Spring对Servlet进行管理,起到一个中转的作用
package com.xwl.estore.servlet;
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;
/**
* 将Servlet转为Spring管理的Servlet Bean
*/
public class ServletToBeanProxy extends GenericServlet {
// 当前客户端请求的Servlet名字
private String targetBean;
// 代理Servlet
private Servlet proxy;
@Override
public void init() throws ServletException {
super.init();
// 初始化Spring容器
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
// 获取Servlet名
this.targetBean = getServletName();
// 调用ServletBean
this.proxy = (Servlet) wac.getBean(targetBean);
// 调用初始化方法将ServletConfig传给Bean
proxy.init(getServletConfig());
}
@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
// 在service方法中调用bean的service方法,servlet会根据客户的请求去调用相应的请求方法(Get/Post)
proxy.service(request, response);
}
}
第3步:在web.xml中注册Servlet
<servlet-name>的名字必须是Spring容器中ServletBean的id
<servlet-class>必须是上面写的代理类的全路径com.xwl.estore.servlet.ServletToBeanProxy
<!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Servlet -->
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.xwl.estore.servlet.ServletToBeanProxy</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
第4步:编写ServletBean
package com.xwl.estore.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.xwl.estore.service.UserService;
@Controller
@Scope("prototype")
public class HelloServlet extends HttpServlet {
private UserService userService;
@Resource
public void setUserService(UserService userService) {
this.userService = userService;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println(userService.sayHello("Hello,Spring.Servlet"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println(userService.sayHello("Hello,Spring.Servlet"));
}
}
第5步:编写Spring配置文件(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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.xwl.estore"></context:component-scan>
</beans>
第6步:进行测试
package com.xwl.estore.service;
public interface UserService {
public String sayHello(String hello);
}
package com.xwl.estore.service.impl;
import org.springframework.stereotype.Service;
import com.xwl.estore.service.UserService;
@Service
public class UserServiceImpl implements UserService {
public String sayHello(String hello) {
return hello;
}
}
原帖地址:
http://blog.csdn.net/xwl617756974/article/details/7451773
http://akhuting.iteye.com/blog/904697