閱讀563 返回首頁    go 魔獸


Spring整合Servlet

在應用中一般普通的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="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:p="https://www.springframework.org/schema/p"
xmlns:aop="https://www.springframework.org/schema/aop" xmlns:context="https://www.springframework.org/schema/context"
xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="
https://www.springframework.org/schema/beans 
https://www.springframework.org/schema/beans/spring-beans-2.5.xsd
https://www.springframework.org/schema/context 
https://www.springframework.org/schema/context/spring-context-2.5.xsd
https://www.springframework.org/schema/tx 
https://www.springframework.org/schema/tx/spring-tx-2.5.xsd
https://www.springframework.org/schema/aop 
https://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;
}
}

原帖地址:
https://blog.csdn.net/xwl617756974/article/details/7451773
https://akhuting.iteye.com/blog/904697


最後更新:2017-04-03 20:19:52

  上一篇:go ibatis運行的SQL語句的輸出——通過配置log4j
  下一篇:go 解決httpclient傳中文亂碼問題