在普通WEB项目中使用Spring

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

时间: 2024-08-26 17:19:58

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

06_在web项目中集成Spring

在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService helloService = (HelloService) applicationContext.getBean("helloS

web项目中 集合Spring&amp;使用junit4测试Spring

web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService helloService = (HelloService) applicationContext.getBean("helloService"); helloService.sayHello(

如何在Web项目中配置Spring MVC

要使用Spring MVC需要在Web项目配置文件中web.xml中配置Spring MVC的前端控制器DispatchServlet 1 <servlet> 2 <servlet-name>SpringMVC</servlet-name> 3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 4 <!-- 默认所对应的配置文件是

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

本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这部分内容需要以下Jar包支持 mysql-connector:MySQL数据库连接驱动,架起服务端与数据库沟通的桥梁: MyBatis:一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架: log4j:Apache的开源项目,一个功能强大的日志组件,提供方便的日志记录: 修改后的pom.xm

maven新建Spring MVC + MyBatis + Oracle的Web项目中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&

在web项目中使用cxf开发webservice,包含spring支持

本文主要介绍了,如何使用cxf内置的例子,学会开发webserivce,在web项目中使用,且包含spring支持. webserivce的开发可以使用cxf或者axis,好像还有httpclient等等.以前也多次研究过,上网搜过很多别人的例子来看,写过代码下来,但没有总结过,少废话,上干货. 1. 到cxf的官网下载jar包.我用的是以前下载下来的apache-cxf-2.7.6.zip,并非最新版本.下载完成后,解压后,目录结构如下左图: 打开其中的samples文件夹,其内包含了很多例子

在java web项目中集成webservice

公司要求在项目中加入webservice服务,因为项目中使用了spring框架,所以在这里使用与spring兼容性较好的cxf来实现 cxf所需jar包 spring的jar包就不贴了 一:创建webservice服务器 1)创建一个服务接口 package com.service; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface IHelloWorld { public S

JAVA WEB项目中各种路径的获取

JAVA WEB项目中各种路径的获取 标签: java webpath文件路径 2014-02-14 15:04 1746人阅读 评论(0) 收藏 举报  分类: JAVA开发(41)  1.可以在servlet的init方法里 String path = getServletContext().getRealPath("/"); 这将获取web项目的全路径 例如 :E:\eclipseM9\workspace\tree\ tree是我web项目的根目录 2.你也可以随时在任意的cla

Maven 创建java Web项目,配置Spring,CXF

1.搭建Maven环境 参考文章 Maven3路程(一)环境搭建 http://www.cnblogs.com/leiOOlei/p/3359561.html Maven3路程(二)Eclipse集成Maven http://www.cnblogs.com/leiOOlei/p/3361379.html Maven3路程(三)用Maven创建第一个web项目(1) Maven3路程(三)用Maven创建第一个web项目(2)servlet演示 Maven 配置Spring 参考 http://b