Springboot web construction yml module annotation of Java framework

Keywords: Java Spring Druid SpringBoot Maven


Spring Boot is a one-stop framework for integrating all application frameworks, simplifying the development of spring applications. It is a runtime application monitoring framework with more conventions than configurations, less complexity and simplicity, and ready to use out of the box, quasi production environment

To quickly build a spring boot application, you must create it online, use Eclipse or Idea, choose a good version of spring boot and Spring Starter Module can

1) automatically generate the main program class to start the project 2) automatically generate the static resource directory and attribute configuration file 3) automatically add the pom.xml related dependency configuration

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <!-- For dependency management, almost all the dependency versions we used have been declared;-->
<!--    All project modules built need to inherit org.springframework.boot Do dependency management-->
<!--    Version Arbitration Center-->
    <!-- spring-boot-starter-xxx: Scene launcher SpringBoot All the dependencies needed to automatically import a scenario-->
    <!-- Introduce springboot Plug in -->

Store static resource files in static folder

The application.properties or application.yml configuration file name is fixed. Define the rules for loading the configuration property file

YAML basic syntax

l use indentation to represent hierarchy

l Tab is not allowed when indenting, only spaces are allowed.

l. the number of indented spaces is not important, as long as the elements of the same level are aligned to the left

l case sensitive

Literal: normal value (number, string, Boolean)

l k: v: literal value can be written directly; string does not need to be enclosed with single quotation mark or double quotation mark by default;

l "": double quotation marks; special characters in strings will not be escaped; special characters will be used as the meaning they want to express example: name: "zhangsan \n lisi": output; zhangsan newline lisi

l '': single quotation mark; will escape special characters, which are only a common string data example: name: 'zhangsan \n lisi': output; zhangsan \n lisi

Object, Map (property and value) (key value pair)

l k: v: write the property and value of the object to indicate its relationship by indenting on the next line; pay attention to indenting

l object or k: v way

l in line writing friends: {lastName: zhangsan,age: 18}

    lastName: zhangsan
    age: 20

Array (List, Set)

l. inline writing pets: [cat,dog,pig]

Represents an element in an array with a - value

    ‐ cat
    ‐ dog

Auto configuration principle

1) SpringBoot helps us configure all application scenarios

2) there will be many xxxautoconfigurarion in spring boot (help us to automatically assemble components in the container)

3) when the xxxautoconfigurarion assigns components to the container, the default properties of the component are generally obtained from the xxxProperties

4). xxxProperties is bound to the configuration file (property one-to-one correspondence)

5) if the default values do not meet our needs, just change these default configurations;

6) if we do not use the default component, we can also customize the component.

One of the biggest strategies of spring boot is to use your own custom components, otherwise, use the default.

Custom configuration class

public class AppConfig {
    public InternalResourceViewResolver internalResourceViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        return resolver;
//    supplement:Obtain ApplicationContext object
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

Spring boot build WEB application project

Build web, JDBC, mybatis, mysql, redis, thmeleaf and other related components

Add Druid data source


application.yml configuration related properties

    username: root
    password: 12345
    url: jdbc:mysql://localhost:3306/scw?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
  config-location: classpath:/mybatis/mybatis-config.xml
  mapper-locations: classpath:/mybatis/mapper/*.xml

Main program configuration class

//Main program scan loading mapper
//Integrated transaction management,stay Service Add in interface@Transactional
//integration Web assembly,Add in configuration class@WebServlet @WebFilter @WebListener
public class SpringMybatisApplication {
    public static void main(String[] args) {
        SpringApplication.run(SptingMybatisApplication.class, args);

Druid configuration class and monitoring background management

public class DruidConfig {
   //Introduce yml File configuration properties
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setFilters("stat");// Configure monitoring statistics intercepted filters
        return dataSource;
    // To configure Druid 1. Configure a management background Servlet
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        Map<String, String> initParams = new HashMap<>();
        initParams.put("loginUsername", "admin");
        initParams.put("loginPassword", "12345");
        initParams.put("allow", "");// The default is to allow all access
        initParams.put("deny", "");// Which one to refuse? ip Visit
        return bean;
    // 2,Configure one web Monitored filter
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        Map<String, String> initParams = new HashMap<>();
        initParams.put("exclusions", "*.js,*.css,/druid/*");// Filter exclusion
        return bean;

Spring Boot common annotation summary

SpringBoot related modules

Posted by ngng on Thu, 05 Mar 2020 01:07:52 -0800