1. Springboot Development, Day 1

Keywords: Programming SpringBoot Spring Java JDBC

SpringBoot Learning, Day 1

Contents: 1, springboot Introduction 2, springboot Integration Serlet 3, springboot Integration filter

I. Introduction to springboot

Spring Boot is designed to simplify the initial build and development of new Spring applications. 
- Embedded Tomcat, without deploying WAR files 
Spring Boot is not an enhancement of Spring functionality, but a way to use Spring quickly.

POM.xml

    <!--springboot The parent class of the project, all springboot Projects must be inherited from it-->
	<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 starter-->
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
	</dependency>
SpringBook starters are actually collections of jar packages. SprigBoot provides a total of 44 starters.
    1 spring-boot-starter-web
    Support full stack web development, including romcat, spring MVC and other jar s
    2 spring-boot-starter-jdbc
    Collection of jar packages supporting spring to manipulate databases in jdbc mode
    3 spring-boot-starter-redis
    Database operations supporting redis key-value storage

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;
    }

}

Startup class

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);
    }

}
@ SpringBoot Application includes:
	@ Annotations such as SpringBootConfiguration, @Enable AutoConfiguration, @ComponentScan, @Configuration, etc., are a configuration class that scans all files under the current package and all subpackages under the current package.

** Result: ** http://localhost:8080/hello

{"msg":"hello world"}

2. Springboot Integration Servlet

There are two ways to register components:

1. Complete component registration by annotation scanning

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 The first way to integrate servlet s is to
 *   Original
 *      <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............");
    }
}

Startup class:

package com.lee;

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

//This annotation scans the @WebServlet under the current package and its subpackage.
//And instantiate the class when it starts
@ServletComponentScan
@SpringBootApplication
public class SpringbootApplicationServlet1 {

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

}

** Result: ** http://localhost:8080/firstServlet

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

2. Complete component registration by method

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....");
    }
}

Startup class

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);
    }

    //Register scondServlet into servlet Registration Bean
    @Bean
    public ServletRegistrationBean secondServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean();
        bean.setServlet(new SecondServlet());
        bean.addUrlMappings("/secondServlet");
        return bean;
    }

}

** Result: ** http://localhost:8080/secondServlet

secondServlet....

3. Springboot Integration Filter

Two ways to register components

1. Complete component registration by annotation scanning

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");
    }
}

Startup class

package com.lee;

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

//This annotation scans the current package and @WebServlet@WebFilter under its subpackage, etc.
//And instantiate the class when it starts
@ServletComponentScan
@SpringBootApplication
public class SpringbootApplicationFilter1 {

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

}

** Result: ** http://localhost:8080/firstServlet

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

2. Complete component registration by method

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");
    }
}

Startup class

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);
    }

    //Register scondServlet into servlet Registration Bean
    @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;
    }

}

Result:

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

Posted by rrijnders on Wed, 09 Oct 2019 08:14:56 -0700