Spring在Web項目中的使用
對于在web項目中使用Spring,一直有想法,但是沒有去實踐,今天心血來潮就試了一次。現在講工程代碼放出來供大家學習。
- 引用jar包
- spring-context
- spring-beans
- spring-core
- spring-web
- spring-aop
- javax.servlet-api
- commons-logging
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.chen</groupId> <artifactId>Spring-In-Webapp</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>Spring-In-Webapp Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- 為spring核心提供了大量擴充。可以找到使用spring applicationcontext特性時所需的全部類, jdni所需的全部類,ui方面的用來與模闆(templating)引擎如 velocity、freemarker、 jasperreports內建的類,以及校驗validation方面的相關類,還有ejb,cache,format,jms等等。 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.12.RELEASE</version> </dependency> <!-- springioc(依賴注入)的基礎實作,所有應用都要用到的,它包含通路配置檔案、建立和管理bean以及 進行inversion of control / dependency injection(ioc/di)操作相關的所有類。 但是這個是個基礎實作,一般我們在實際的開發過程中很少直接用到,它是對起到支撐作用的。 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.12.RELEASE</version> </dependency> <!-- spring的核心包,包含spring架構基本的核心工具類,spring其它元件要都要使用到這個包裡的類,是其它 元件的基本核心。包括asm,cglib以及相關的工具類 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.12.RELEASE</version> </dependency> <!-- 包含web應用開發時,用到spring架構時所需的核心類,包括自動載入webapplicationcontext特性的類、 struts與jsf內建類、檔案上傳的支援類、filter類和大量工具輔助類。 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.12.RELEASE</version> </dependency> <!-- 包含在應用中使用spring的aop特性時所需的類。 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.12.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> <build> <finalName>Spring-In-Webapp</finalName> </build> </project>
- 項目結構
Spring在web中的使用Spring在Web項目中的使用 - 代碼
- HelloService.java
package service; /** * Created by handsome programmer. * User: chen * Date: 2018/1/21 * Time: 17:38 * Description: */ public class HelloService { public String getUsername(String str) { return "Hello " + str; } }
- HelloSpring.java
package servlet; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import service.HelloService; 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; /** * Created by handsome programmer. * User: chen * Date: 2018/1/21 * Time: 17:24 * Description: */ @WebServlet(name = "hello", urlPatterns = "/hello") public class HelloSpring extends HttpServlet { private HelloService helloService; @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String message = request.getParameter("username"); //擷取Spring容器 WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext()); System.out.println("applicationContext = " + applicationContext); helloService = applicationContext.getBean("helloService", HelloService.class); System.out.println("message = " + message); message = helloService.getUsername(message); request.setAttribute("message", message); request.getRequestDispatcher("index.jsp").forward(request, response); } }
- 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="helloService" class="service.HelloService"/> </beans>
- web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
- index.jsp
<%-- Created by IntelliJ IDEA. User: chen Date: // Time: : To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Hehe</title> </head> <body> <h1> <%=request.getAttribute("message")%> </h1> </body> </html>
- 通路連結:
http://localhost:8080/hello?username=admin
- 通路效果
背景Spring在web中的使用Spring在Web項目中的使用 Spring在web中的使用Spring在Web項目中的使用 - HelloService.java