Spring是一个对象容器,帮助咱们管理项目中的对象,那么在web项目中哪些对象应该交给Spring管理呢?java
咱们回顾一下WEB项目中涉及的对象web
咱们在学习IOC容器时知道,Spring能够帮助咱们管理原来须要本身建立管理的对象,若是一个对象原来就不须要咱们自行管理其的建立和声明周期的话,那么该对象也就不须要交给Spring管理spring
由此来看,上述对象中只有Service,DAO,POJO应该交给Spring管理,而因为POJO一般都是由持久层框架动态建立的,因此最后只有Service和DAO要交给Spring容器管理,api
固然Spring中的AOP也是很经常使用的功能,例如事务管理,日志输出等..tomcat
<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项目同样进行开发了app
<?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>
在容器中添加了一个整数,用于测试容器是否可用框架
<!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>
测试Servletjsp
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项目都正常工做了!maven