spring 整合 servlet


目的:記錄spring整合 servlet過程demo。(企業實際開發中可能很少用到),融會貫通。

前言:在學習spring 過程(核心 ioc,aop,插一句 學了spring 才對這個有深刻概念, 在net時候都是直接使用,不得不說 java 還是深刻點)過程中,我們基本上都是在test中測試如圖

環境:IDEA 

 

 

 

但是,開發中是 spring容器 是共享的,其他地方直接調用,這里就涉及到spring和其他的整合,此文servlet 為測試點。

 

1:新建servlet 過程參考,https://www.cnblogs.com/y112102/p/11338610.html

 

2:導入jar包。(4個核心,一個依賴)后期開發推薦使用 Maven,后面在測試,示logging找不到  , 日志也要導入

   2.1: spring-web 也要記得

在復習一遍:

 

1:    基礎:4+1 , beans、core、context、expression , commons-logging 
2:    AOP:aop聯盟、spring aop 、aspect規范、spring aspect
3:    db:jdbc、tx(事務)
4:    測試:test
5:    web開發:spring web

 

 

 

 

 

 

 

 

 3:web.xml 配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!-- 確定配置文件位置:
        classpath: src 或者classes 目錄下-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- 配置spring 監聽器,加載xml配置文件
         目的:tomcat啟動的時候會自動根據配置文件創建 spring對象-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>HelloServlet</servlet-class>
    </servlet>

    <!-- 瀏覽器訪問 hello 找到HelloServlet 請求  -->
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

</web-app>

  

 3.2 參考3.1配置文件 新建 HelloServlet 不要過多解釋,如圖:

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
/**
 * Create by  on 2019-09-10
 *
 * @author lsw
 */
public class HelloServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response){

        try {

            // 從application作用域(ServletContext)獲得spring容器
            //方式1: 手動從作用域獲取 getAttribute返回的Object類型 需要強轉
            ApplicationContext applicationContext =
                    (ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

            //方式2:通過工具獲取
            ApplicationContext apppApplicationContext2 =
                    WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());


            //從容器中取Bean的實例
            var c =  applicationContext.getBean("AddressId", Address.class);
            System.out.println(  c.toString() );

            response.getWriter().println("<h1>Hello Servlet!</h1>");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

 

最后啟動運行結果:

 

 

控制台:

 

 

 

 

 

總結:

servlet :web層,這里將來是spring mvc ,有很多選擇 比如 struts(現在也很少用到了)

mybatis:dao層,知識點雜

spring:service層

java 入門 堅持下,你會發現很簡單的,特別是net開發者,接觸java 后才去理解net的開發方式 又會有另一番心得。

所有的都是圍繞次服務 ,擴展的。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM