Spring是一個對象容器,幫助我們管理項目中的對象,那么在web項目中哪些對象應該交給Spring管理呢?
項目中涉及的對象
我們回顧一下WEB項目中涉及的對象
- Servlet
- Request
- Response
- Session
- Service
- DAO
- POJO
分析
我們在學習IOC容器時知道,Spring可以幫助我們管理原來需要自己創建管理的對象,如果一個對象原來就不需要我們自行管理其的創建和聲明周期的話,那么該對象也就不需要交給Spring管理
由此來看,上述對象中只有Service,DAO,POJO應該交給Spring管理,而由於POJO通常都是由持久層框架動態創建的,所以最后只有Service和DAO要交給Spring容器管理,
當然Spring中的AOP也是很常用的功能,例如事務管理,日志輸出等..
最小pom依賴:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!-- spring核心容器-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- spring-web-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!--web相關的-->
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
上述依賴可以保證項目可以使用Spring容器以及JSP,EL,Servlet的正常使用,測試完成后既可以向普通Spring或web項目一樣進行開發了
Spring配置文件:
<?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-->
<bean id="integer" class="java.lang.Integer">
<constructor-arg type="java.lang.String" value="100"/>
</bean>
</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 version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- spring配置文件:-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring監聽器 使得容器隨着項目啟動:-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
測試Servlet
package com.yh.servlet;
import org.springframework.web.context.WebApplicationContext;
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(name = "TestServlet",urlPatterns = "/test")
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取Spring容器
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
//獲取容器中的對象測試
Object integer = context.getBean("integer");
response.getWriter().println("hello spring "+integer);
}
}
配置tomcat啟動訪問:http://localhost:8080/HMWK_war_exploded/test即可查看到容器中的整數100,表明Spring和WEB項目都正常工作了!