5-03.web-serlvet和spring的整合讲解

\web-servlet-spring\src\ applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <!--xmlns xml namespace:xml命名空间--> <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:context ="http://www.springframework.org/schema/context" xmlns:aop ="http://www.springframework.org/schema/aop" xmlns:tx ="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="userService" class="com.gyf.service.impl.UserServiceImpl"></bean> </beans>
\web-servlet-spring\web\WEB-INF\ web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 配置spring配置文件的加载路径--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--监听器--> <listener> <!--配置是 [/WEB-INF/applicationContext.xml]路径下的文件--> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>

web-servlet-spring\src\com\gyf\web\servlet \ RegisterServlet.java
package com.gyf.web.servlet; import com.gyf.service.IUserService; import com.gyf.service.impl.UserServiceImpl; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/register") public class RegisterServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取参数 String username = request.getParameter("username"); //调用service //service的创建,由spring创建,从spring的容器获取user IUserService userService = null; /*开发中,项目只需要加载一次spring的配置文件就可以了*/ /*ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); */ //取applicationcontext对象 ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext()); System.out.println(context.hashCode()); userService = (IUserService) context.getBean("userService"); userService.add(username); //响应 response.getWriter().write("register success"); } }

浙公网安备 33010602011771号