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依赖:

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <maven.compiler.source>1.7</maven.compiler.source>
  4. <maven.compiler.target>1.7</maven.compiler.target>
  5. </properties>
  6. <dependencies>
  7. <!-- spring核心容器-->
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-context</artifactId>
  11. <version>5.2.2.RELEASE</version>
  12. </dependency>
  13. <!-- spring-web-->
  14. <dependency>
  15. <groupId>org.springframework</groupId>
  16. <artifactId>spring-web</artifactId>
  17. <version>5.2.2.RELEASE</version>
  18. </dependency>
  19. <!--web相关的-->
  20. <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
  21. <dependency>
  22. <groupId>javax.servlet</groupId>
  23. <artifactId>jstl</artifactId>
  24. <version>1.2</version>
  25. </dependency>
  26. <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
  27. <dependency>
  28. <groupId>javax.servlet.jsp</groupId>
  29. <artifactId>javax.servlet.jsp-api</artifactId>
  30. <version>2.3.3</version>
  31. <scope>provided</scope>
  32. </dependency>
  33. <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  34. <dependency>
  35. <groupId>javax.servlet</groupId>
  36. <artifactId>javax.servlet-api</artifactId>
  37. <version>4.0.1</version>
  38. <scope>provided</scope>
  39. </dependency>
  40. </dependencies>

上述依赖可以保证项目可以使用Spring容器以及JSP,EL,Servlet的正常使用,测试完成后既可以向普通Spring或web项目一样进行开发了

Spring配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!-- 测试bean-->
  6. <bean id="integer" class="java.lang.Integer">
  7. <constructor-arg type="java.lang.String" value="100"/>
  8. </bean>
  9. </beans>

在容器中添加了一个整数,用于测试容器是否可用

web.xml

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app version="2.5"
  5. xmlns="http://java.sun.com/xml/ns/javaee"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  8. <display-name>Archetype Created Web Application</display-name>
  9. <welcome-file-list>
  10. <welcome-file>index.jsp</welcome-file>
  11. </welcome-file-list>
  12. <!-- spring配置文件:-->
  13. <context-param>
  14. <param-name>contextConfigLocation</param-name>
  15. <param-value>classpath:applicationContext.xml</param-value>
  16. </context-param>
  17. <!-- spring监听器 使得容器随着项目启动:-->
  18. <listener>
  19. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  20. </listener>
  21. </web-app>

测试Servlet

  1. package com.yh.servlet;
  2. import org.springframework.web.context.WebApplicationContext;
  3. import org.springframework.web.context.support.WebApplicationContextUtils;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. @WebServlet(name = "TestServlet",urlPatterns = "/test")
  11. public class TestServlet extends HttpServlet {
  12. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. }
  14. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  15. //获取Spring容器
  16. WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
  17. //获取容器中的对象测试
  18. Object integer = context.getBean("integer");
  19. response.getWriter().println("hello spring "+integer);
  20. }
  21. }

配置tomcat启动访问:http://localhost:8080/HMWK_war_exploded/test即可查看到容器中的整数100,表明Spring和WEB项目都正常工作了!

在普通WEB项目中使用Spring的更多相关文章

  1. web项目中 集合Spring&使用junit4测试Spring

    web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...

  2. 06_在web项目中集成Spring

    在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = ...

  3. 如何在web项目中配置Spring的Ioc容器

    在web项目中配置Spring的Ioc容器其实就是创建web应用的上下文(WebApplicationContext) 自定义要使用的IoC容器而不使用默认的XmlApplicationContext ...

  4. Axis2在Web项目中整合Spring

    一.说明: 上一篇说了Axis2与Web项目的整合(详情 :Axis2与Web项目整合)过程,如果说在Web项目中使用了Spring框架,那么又改如何进行Axis2相关的配置操作呢? 二.Axis2 ...

  5. web项目中获取spring的bean对象

    Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中不通过注解的形式(@Resource.@Autowired)获取Spring配置的bean呢? Bean工厂(c ...

  6. 如何在Web项目中配置Spring MVC

    要使用Spring MVC需要在Web项目配置文件中web.xml中配置Spring MVC的前端控制器DispatchServlet <servlet> <servlet-name ...

  7. java web项目中引入spring

    自己动手实践了一次,发生中间出了一下问题,现整理出来,供参考. Step1: 新建一个java web项目 Step2:下载spring的jar包http://repo.spring.io/libs- ...

  8. web项目中加入struts2、spring的支持,并整合两者

    Web项目中加入struts2 的支持 在lib下加入strut2的jar包 2. 在web.xml中添加配置 <filter> <filter-name>struts2< ...

  9. Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问

    本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...

随机推荐

  1. PAT T1010 Lehmer Code

    跟1009几乎是同一道题~ #include<bits/stdc++.h> using namespace std; ; int a[maxn]; ]; int r[maxn]; int ...

  2. Struts笔记二:栈值的内存区域及标签和拦截器

    值栈和ognl表达式 1.只要是一个MVC框架,必须解决数据的存和取的问题 2.struts2利用值栈来存数据,所以值栈是一个存储数据的内存结构 1.  ValueStack是一个接口,在struts ...

  3. day5-1继承

    继承: Constructor属性和prototype属性的关系: 创建了自定义的构造函数之后,其原型对象默认会取得constructor属性:当调用构造函数创建一个新实例后,该实例的内部将包含一个指 ...

  4. 寒假作业---蓝桥杯---DFS

    题目描述 现在小学的数学题目也不是那么好玩的. 看看这个寒假作业: 每个方块代表1~13中的某一个数字,但不能重复. 比如: 6  + 7 = 13 9  - 8 = 1 3  * 4 = 12 10 ...

  5. Spark程序编译报错error: object apache is not a member of package org

    Spark程序编译报错: [INFO] Compiling 2 source files to E:\Develop\IDEAWorkspace\spark\target\classes at 156 ...

  6. 树莓派4B踩坑指南 - (4)输入法和字体

    输入法和字体 fcitx 安装谷歌输入法和sunpinyin,哪个不用可以装完卸载: sudo apt-get install fcitx fcitx-googlepinyin fcitx-modul ...

  7. linux搭建mysql时ifconfig命令无法使用问题

    刚搭建好的Centos 最小安装模式是没有ifconfig命令的.改变步骤:一:使用语句:cd /etc/sysconfig/network-scripts/二:使用语句vi ifcfg-eno167 ...

  8. 63 滑动窗口的最大值 &&front(),back()操作前一定要判断容器的尺寸不能为0

    给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6, ...

  9. SpringMVC 接收表单数据、数据绑定、解决请求参数中文乱码

    接收表单数据有3种方式. 1.使用简单类型接收表单数据(绑定简单数据类型) 表单: <form action="${pageContext.request.contextPath}/u ...

  10. 科软-信息安全实验1-ICMP重定向

    目录 一 前言 二 Talk is cheap, show me the code 三 效果演示 四 遇到的问题&解决 一 前言 文章不讲解理论知识哈,想学习理论知识的,认真听课