1. 程式人生 > >1、Springboot開發,第一天

1、Springboot開發,第一天

SpringBoot學習,第一天

目錄:1、springboot入門 2、springboot整合servlet 3、springboot整合filter

一、springboot入門

•  Spring Boot設計目的是用來簡化新 Spring 應用的初始搭建以及開發過程。 
•  嵌入的 Tomcat,無需部署 WAR 檔案 
•  Spring Boot 並不是對 Spring 功能上的增強,而是提供了一種快速使用 Spring 的方式。

POM.xml

    <!--springboot專案的父類,所有springboot專案都必須繼承於它-->
	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

	<!--springboot啟動器-->
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
	</dependency>
springBoot啟動器其實就是一些jar包的集合。SprigBoot一共提供44啟動器。
    1 spring-boot-starter-web
    支援全棧式的web開發,包括了romcat和springMVC等jar
    2 spring-boot-starter-jdbc
    支援spring以jdbc方式操作資料庫的jar包的集合
    3 spring-boot-starter-redis
    支援redis鍵值儲存的資料庫操作

Controller

package com.lee.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class HelloWorld {

    @RequestMapping("/hello")
    public Map<String,Object> hello(){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("msg","hello world");
        return resultMap;
    }

}

啟動類

package com.lee;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }

}
@SpringBootApplication包含:
	@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan、@Configuration等註解,它是一個配置類,掃描了當前包和當前包下所有子包下的所有檔案

**結果:**http://localhost:8080/hello

{"msg":"hello world"}

二、Springboot整合Servlet

兩種方式完成元件的註冊:

1、通過註解掃描完成元件的註冊

FirstServlet

package com.lee.servlet;

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;

/**
 * springboot整合servlet的第一種方式:
 *   原來
 *      <servlet>
 *          <servlet-name>firstServlet</servlet-name>
 *          <servlet-class>com.lee.FirstServlet</servlet-class>
 *      </servlet>
 *      <servlet-mapping>
 *          <servlet-name>firstServlet</servlet-name>
 *          <url-pattern>/firstServlet</url-pattern>
 *      </servlet-mapping>
 */
@WebServlet(name="firstServlet",urlPatterns = "/firstServlet")
public class FirstServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("firstServlet............");
    }
}

啟動類:

package com.lee;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

//該註解會掃描當前包和其子包下的 @WebServlet,
//並在啟動類啟動的時候將其例項化
@ServletComponentScan
@SpringBootApplication
public class SpringbootApplicationServlet1 {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplicationServlet1.class,args);
    }

}

**結果:**http://localhost:8080/firstServlet

firstServlet............

2、通過方法完成元件的註冊

SecondServlet

package com.lee.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SecondServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("secondServlet....");
    }
}

啟動類

package com.lee;

import com.lee.servlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringbootApplicationServlet2 {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplicationServlet2.class,args);
    }

    //將scondServlet註冊到servletRegistrationBean中
    @Bean
    public ServletRegistrationBean secondServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean();
        bean.setServlet(new SecondServlet());
        bean.addUrlMappings("/secondServlet");
        return bean;
    }

}

**結果:**http://localhost:8080/secondServlet

secondServlet....

三、Springboot整合Filter

兩種方式完成元件的註冊

1、通過註解掃描完成元件的註冊

FirstFilter

package com.lee.filter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * <filter>
 *     <filter-name>FirstFilter</filter-name>
 *     <filter-class>com.lee.filter.FirstFilter</filter-class>
 * </filter>
 * <filter-mapping>
 *     <filter-name>FirstFilter</filter-name>
 *     <url-patter>/firstServlet</url-patter>
 * </filter-mapping>
 */
//@WebFilter(filterName = "firstFilter",urlPatterns = {"*.do","*.action"})
@WebFilter(filterName = "firstFilter",urlPatterns = "/firstServlet")
public class FirstFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println(" first filter init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("enter first filter");
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("leave first filter");
    }

    @Override
    public void destroy() {
        System.out.println(" first filter destroy");
    }
}

啟動類

package com.lee;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

//該註解會掃描當前包和其子包下的 @WebServlet @WebFilter等,
//並在啟動類啟動的時候將其例項化
@ServletComponentScan
@SpringBootApplication
public class SpringbootApplicationFilter1 {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplicationFilter1.class,args);
    }

}

**結果:**http://localhost:8080/firstServlet

first filter init
enter first filter
firstServlet............
leave first filter

2、通過方法完成元件的註冊

SecondFilter

package com.lee.filter;

import javax.servlet.*;
import java.io.IOException;

public class SecondFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println(" second filter init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("enter second filter");
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("leave second filter");
    }

    @Override
    public void destroy() {
        System.out.println(" second filter destroy");
    }
}

啟動類

package com.lee;

import com.lee.filter.SecondFilter;
import com.lee.servlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringbootApplicationFilter2 {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplicationFilter2.class,args);
    }

    //將scondServlet註冊到servletRegistrationBean中
    @Bean
    public ServletRegistrationBean secondServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean();
        bean.setServlet(new SecondServlet());
        bean.addUrlMappings("/secondServlet");
        return bean;
    }

    @Bean
    public FilterRegistrationBean secondFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new SecondFilter());
        bean.addUrlPatterns("/secondServlet");
        return bean;
    }

}

結果:

second filter init
enter second filter
secondServlet....
leave se