Springboot web construction yml module annotation of Java framework

Keywords: Java Spring Druid SpringBoot Maven

SpringBoot

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">
    <modelVersion>4.0.0</modelVersion>

    <!-- For dependency management, almost all the dependency versions we used have been declared;-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.12.RELEASE</version>
        <relativePath/>
    </parent>
<!--    All project modules built need to inherit org.springframework.boot Do dependency management-->
    <groupId>cn.gcaca</groupId>
    <artifactId>hello</artifactId>
    <version>0.0.1-SNAPSHOT</version>
<!--    Version Arbitration Center-->
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <!-- spring-boot-starter-xxx: Scene launcher SpringBoot All the dependencies needed to automatically import a scenario-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <!-- Introduce springboot Plug in -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

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}

friends:
    lastName: zhangsan
    age: 20

Array (List, Set)

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

Represents an element in an array with a - value

pets:
    ‐ 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

@SpringBootConfiguration
public class AppConfig {
    @Bean
    public InternalResourceViewResolver internalResourceViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/templates/");
        resolver.setSuffix(".html");
        return resolver;
    }
//    supplement:Obtain ApplicationContext object
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        context.getBean("myBean");
    }
}

Spring boot build WEB application project

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

Add Druid data source

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>

application.yml configuration related properties

spring:
  datasource:
    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
mybatis:
  config-location: classpath:/mybatis/mybatis-config.xml
  mapper-locations: classpath:/mybatis/mapper/*.xml

Main program configuration class

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

Druid configuration class and monitoring background management

@SpringBootConfiguration
public class DruidConfig {
   //Introduce yml File configuration properties
    @Bean
    @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
    @Bean
    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
        bean.setInitParameters(initParams);
        return bean;
    }
    // 2,Configure one web Monitored filter
    @Bean
    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
        bean.setInitParameters(initParams);
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }
}

Spring Boot common annotation summary

SpringBoot related modules

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