spring与servlet环境配置

1、HttpServletProxy.java

package com.zfy.commons.util;

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 org.springframework.web.context.support.WebApplicationContextUtils;

public class HttpServletProxy extends HttpServlet {

	private static final long serialVersionUID = 4382945551447764730L;

	private String targetServlet;

	private HttpServlet proxy;

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void init() throws ServletException {

		this.targetServlet = getInitParameter("targetServlet");
		getServletBean();
		proxy.init(getServletConfig());
	}

	private void getServletBean() {
		WebApplicationContext wac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());
		this.proxy = (HttpServlet) wac.getBean(targetServlet);

	}
    @Override
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, RuntimeException {

		proxy.service(request, response);
	}
}
2、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value>
	</context-param>
	<filter>
		<description>字符集过滤器</description>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<description>字符集编码</description>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>

	<listener>
		<description>spring监听器</description>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 防止spring内存溢出监听器 -->
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
	<!-- 修复数据库监听器 
	<listener>
		<listener-class>sy.listener.RepairListener</listener-class>
	</listener>
	--><!-- 在线用户监听器 -->
	<!--<listener>
		<listener-class>sy.listener.OnlineListener</listener-class>
	</listener>
	--><!-- spring mvc servlet -->
	<servlet>
		<description>spring mvc servlet</description>
		<servlet-name>springMvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<description>spring mvc 配置文件</description>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

  
  <!-- 开始 -->
  	<!-- <param-value>auditServlet</param-value>对应Controller -->

  
 
  <servlet>
    <servlet-name>AuditServlet</servlet-name>
  <servlet-class>com.zfy.commons.util.HttpServletProxy</servlet-class>
    <init-param>
    	<param-name>targetServlet</param-name>
    	<param-value>auditServlet</param-value>
    </init-param>
  </servlet>
  
 
  <servlet-mapping>
    <servlet-name>AuditServlet</servlet-name>
    <url-pattern>/AuditServlet</url-pattern>
  </servlet-mapping>

<!-- 结束 -->


	<servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
  
	

</web-app>

3、Spring.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">


<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:config.properties" />


<!-- 自动扫描dao和service包(自动注入) -->
<context:component-scan base-package="com.zfy.db.dao,com.zfy.service" />


      <!-- 注入servlet -->
    <bean id="auditServlet" class="com.zfy.servlet.securityCenter.AuditServlet"  / >

</beans>
4、接口层(services)

public interface AuditService {

  .....
}
5、实例类(impl)

@Service
public class AuditImpl implements AuditService {
......
}
6、Servlet 

注意:web.xml、spring和Servlet的那个 auditService必须一致

@Controller("auditServlet")
public class AuditServlet extends HttpServlet {

	
	@Resource
	private AuditService auditService;

}
 
 
 
 
 
 
 
 
 
 
 
 

                
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值