Spring在Web项目中的使用
对于在web项目中使用Spring,一直有想法,但是没有去实践,今天心血来潮就试了一次。现在讲工程代码放出来供大家学习。
引用jar包
- spring-context
- spring-beans
- spring-core
- spring-web
- spring-aop
- javax.servlet-api
- commons-logging
pom.xml
<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>
项目结构
代码
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: 2018/1/21 Time: 17:44 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
- 访问效果
网页
后台