在普通WEB項目中使用Spring

Spring是一個對象容器,幫助咱們管理項目中的對象,那麼在web項目中哪些對象應該交給Spring管理呢?java

項目中涉及的對象

​ 咱們回顧一下WEB項目中涉及的對象web

  • Servlet
  • Request
  • Response
  • Session
  • Service
  • DAO
  • POJO

分析

咱們在學習IOC容器時知道,Spring能夠幫助咱們管理原來須要本身建立管理的對象,若是一個對象原來就不須要咱們自行管理其的建立和聲明週期的話,那麼該對象也就不須要交給Spring管理spring

由此來看,上述對象中只有Service,DAO,POJO應該交給Spring管理,而因爲POJO一般都是由持久層框架動態建立的,因此最後只有Service和DAO要交給Spring容器管理,api

固然Spring中的AOP也是很經常使用的功能,例如事務管理,日誌輸出等..tomcat

最小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項目同樣進行開發了app

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>

測試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

相關文章
相關標籤/搜索