SpringBoot (simple modification configuration)

1.Spring Initializer quickly creates a Spring Boot project
Insert picture description here

src/main/java ----Save the java source code
src/main/resources
application.properties- ------The configuration file of the Spring Boot application
[static] —You need to create it manually [Save the required web application Static resources {html, css, js, img}]
[templates]
-You need to manually create [save template page] Spring Boot's default jar package uses embedded Tomcat, which does not support JSP pages by default; template engines freemarker and thymeleaf can be used;
Relying on the @EnableAutoConfiguration included in the @SpringBootApplication annotation, the SpringBoot project will create automatic default configuration data to ensure that the SpringBoot project can run normally without any programming operations after it is successfully created.
The default data values ​​of the automatic configuration used by the SpringBoot project in different environments need to be modified as the environment changes. We cannot modify the source code when modifying, and the source code page cannot be modified. Based on this situation, the SpringBoot project provides external A file that can be used to modify the default data value of the automatic configuration, this file is the src/main/resources/application.properties file.
application.properties file SpringBoot's core configuration file
Function: The file that modifies the default data value of the automatic configuration
Name: application.properties / application.yml
application.properties /application.yml is the same configuration file, and the different suffixes indicate that the writing style of the content in this file is different.
For example: configuration database driver name
application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver

YAML (YAML Ain't Markup Language)
YAML A Markup Language: is a markup language
Speaking of markup languages ​​we think of html/xml, they are all markup languages.
Html [Hypertext Markup Language] ---- markup is provided by default, for example:


Xml [Extensible Markup Language] - to manually create tags such as:
for example
Xml: configuration examples

<server> 
<port>8081</port> 
</server>
YAML:配置例子
server:
Port:8081

YAML isn't Markup Language: not a markup language; because it is data-centric

YAML syntax format:
1. Basic syntax
Key: (space) value: indicates a pair of key-value pairs (spaces must be present); the
hierarchical relationship is controlled by the indentation of spaces; as long as a column of data is left-aligned, they are all at the same level .
E.g:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test
  jdbc:
    template:
      query-timeout: 1000
server:
  port: 9090

2. Attribute worth writing
Attribute value-the value corresponding to the key of the key-value pair
In the above example, url/port are all attributes, which are actually member variables of the xxxxxProperties class. "Jdbc:mysql://127.0.0.1:3306/test, 9090" is the attribute value.
Since the properties of the yml file are actually member variables of the xxxxxProperties class, member variables have fixed data types, so the value of the property cannot be written casually, and must conform to the data type corresponding to the member variable.
1. Ordinary value (number, string, Boolean)
1.1 Number-number value
1.2 Boolean-true/false
1.3 String The
default single or double quotation mark is not required;
if there is "" [double quotation mark], the value in the string The escape character will be executed [\n—line feed]
For example: name: "zhangsan\nlisi"
Result:
zhangsan \n—line feed
lisi
If there is ”[single quote], the escape character in the string will not be executed, it will be used as One character is directly used [\n-\n]
For example: name:'zhangsan\nlisi'
Result: zhangsan\nlisi
2. Object
2.1 Object name The attribute in the object is expressed by line break and indentation.
For example:

student: 
  		stuid: 1001
  		stuname: zhangsan
  		stuage: 23
  		stuaddress: 西安

student----object name
stuid, stuname, stuage, stuaddress----attribute in the object
1001, zhangsan, 23, Xi'an----data value of attribute
2.2 object name: (attribute name: attribute value, attribute name :Attribute value}
For example: [inline writing]

person: {perid:1002,pername: lisi,perage: 24,peraddress: 北京}

3. Collection
Array type collection (List, Set)
1. "-[Space] Element value in array collection"
For example:

javas: 
  		- javase
  		- javaee
 		- javame

javas----array name
javase, javaee, javame—data value in the array collection
2. Array name: [data value 1, data value 2]
For example: inline writing

names: [zhangsan,lisi,wangwu]

Map (key-value pair):
The name of the Map collection: {key1:value1,key2:value2}
For example: maps: {name: zhangsan,age: 23,address: Xi'an}
3. Bind the data value in the YAML file to On javabean,
use @ConfigurationProperties to bind the data values ​​in the core configuration file to the member variables in the javabean class.
@ConfigurationProperties has a property prefix
prefix = "object name in the YAML file".
Specific steps:
1. Create a project.
SelectInsert picture description here
Insert picture description here

2. Create javabean
3. Add annotation
@Component
@ConfigurationProperties(prefix = "student") on Javabean

package com.lx.springboot.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "student")
public class StudentBean {
    
    
    private Integer stuid;
    private String stuname;
    private boolean stusex;
    private List<String> likes;
    private Map<String,String>maps;
    private MyAddress myAddress;

    public Integer getStuid() {
    
    
        return stuid;
    }

    public void setStuid(Integer stuid) {
    
    
        this.stuid = stuid;
    }

    public String getStuname() {
    
    
        return stuname;
    }

    public void setStuname(String stuname) {
    
    
        this.stuname = stuname;
    }

    public boolean isStusex() {
    
    
        return stusex;
    }

    public void setStusex(boolean stusex) {
    
    
        this.stusex = stusex;
    }

    public List<String> getLikes() {
    
    
        return likes;
    }

    public void setLikes(List<String> likes) {
    
    
        this.likes = likes;
    }

    public Map<String, String> getMaps() {
    
    
        return maps;
    }

    public void setMaps(Map<String, String> maps) {
    
    
        this.maps = maps;
    }

    public MyAddress getMyAddress() {
    
    
        return myAddress;
    }

    public void setMyAddress(MyAddress myAddress) {
    
    
        this.myAddress = myAddress;
    }
}

4. The controller class used to test

package com.lx.springboot.controller;
import com.lx.springboot.bean.MyAddress;
import com.lx.springboot.bean.StudentBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
@Controller
public class StudentController {
    
    
    @Autowired
    private StudentBean studentBean;
    @RequestMapping(value = "/getStuInfo")
    @ResponseBody
    public String getStuInfo(){
    
    
        System.out.println("student=="+studentBean);
        int stuid=studentBean.getStuid();
        String stuname = studentBean.getStuname();
        String stuinfo = stuid+" "+stuname;
        List<String> likes = studentBean.getLikes();
        for (String like:likes){
    
    
            System.out.println("like=="+like);
        }
        Map<String,String>maps = studentBean.getMaps();
        for (Map.Entry<String,String>entry:maps.entrySet()){
    
    
            System.out.println(entry.getKey()+" "+entry.getValue());
        }
        MyAddress myAddress=studentBean.getMyAddress();
        System.out.println(myAddress.getAddressid()+" "+myAddress.getAddressname()+" "+myAddress.getAddressinfo());
        return stuinfo;
    }
}

5. Configure properties in the application.yml of resources

student:
  stuid: 1001
  stuname: zhangsan
  stusex: true
  likes:
    - game
    - book
    - play
  maps: {
    
    xiaoxue: 长安县,zhongxue: 雁塔区}
  my-address: {
    
    addressid: 1,addressname: work,addressinfo: 桃园南路}

Configure properties in application.properties of resources

student.stuid=1001
student.stuname=zhangsan
student.stusex=true
student.likes=game,book,play
#student.likes[0]=game
#student.likes[1]=book
#student.likes[2]=play
student.maps.xiaoxue=长安县
student.maps.zhongxue=雁塔区
student.my-address.addressid=1
student.my-address.addressname=home
student.my-address.addressinfo=桃园南路

Insert picture description here

Because the properties configuration file in the idea default utf-8 may have Chinese garbled, so you need to set and modify the transcoding;
Insert picture description here

Controller class for testing

package com.wangxing.springboot.controller;
import com.wangxing.springboot.bean.MyAddress;
import com.wangxing.springboot.bean.StudentBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class StudentController {
    @Autowired
    private StudentBean studentBean;

    @RequestMapping(value = "/getStuInfo")
    @ResponseBody
    public String getStuInfo(){
        System.out.println("student=="+studentBean);
        int stuid=studentBean.getStuid();
        String stuname=studentBean.getStuname();
        String stuinfo=stuid+" "+stuname;
        List<String> likes=studentBean.getLikes();
        for(String like:likes){
            System.out.println("like=="+like);
        }
        Map<String,String> maps=studentBean.getMaps();
        for(Map.Entry<String,String> entry:maps.entrySet()){
            System.out.println(entry.getKey()+"   "+entry.getValue());
        }
        MyAddress myAddress= studentBean.getMyAddress();
        System.out.println(myAddress.getAddressid()+"   "+myAddress.getAddressname());
        return stuinfo;
    }
}

Bind the data value with the member variable in the javabean class through @Value

package com.wangxing.springboot.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class PersonBean {
    @Value("1001")
    private int perid;
    @Value("zhangsan")
    private String pername;

    public int getPerid() {
        return perid;
    }

    public void setPerid(int perid) {
        this.perid = perid;
    }

    public String getPername() {
        return pername;
    }

    public void setPername(String pername) {
        this.pername = pername;
    }
}

Test code
package com.wangxing.springboot.controller;

import com.wangxing.springboot.bean.PersonBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PersonController {
    @Autowired
    private PersonBean personBean;

    @RequestMapping(value = "/getPersonInfo")
    @ResponseBody
    public String getPersonInfo(){
        int id=personBean.getPerid();
        String name=personBean.getPername();
        System.out.println(id+" "+name);
        return "hello";
    }
}

Insert picture description here

@Value get value and @ConfifigurationProperties get value comparison
Insert picture description here

Both the configuration file yml and properties can get the value;
if we just need to get a certain value in the configuration file in a certain business logic, use @Value;
if we say, we have specially written a javaBean to and configure For file mapping, we directly use @ConfifigurationProperties;

Guess you like

Origin blog.csdn.net/guoguo0717/article/details/110677840