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项目都正常工作了!
来源:https://www.cnblogs.com/yangyuanhu/p/12259096.html