当前位置:网站首页>Section 6: basic configuration I of spingboot

Section 6: basic configuration I of spingboot

2022-06-23 02:44:00 Introductory notes

SpringBoot The default configuration file is application.properties perhaps application.yml, This file will be automatically loaded when the application starts , No need to introduce it manually .

Custom properties

stay application.properties Define attribute values in

# Custom properties 
rumenz.name=rumenz
rumenz.url=https://rumenz.com

adopt @Value Annotation to get the value

@RestController
@RequestMapping("/rumenz")
public class RumenzController {
     @Value("${rumenz.name}")
     private String rumenzName;

     @Value("${rumenz.url}")
     private String rumenzUrl;

     @RequestMapping("/index")
     public String index(){
          return rumenzName+":::"+rumenzUrl;
     }
}

visit http://127.0.0.1:8080/rumenz/index route , Returns the rumenz:::https://rumenz.com.

Configure the binding

There are many attributes that need to be injected when there is time , Binding one by one is tedious , The official suggestion is that Bean How to load .

application.properties The configuration file defines attribute values

com.rumenz.id=1
com.rumenz.name=hello

RumenzConfig The configuration file

@Component
@ConfigurationProperties(prefix = "com.rumenz")
public class RumenzConfig {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

test

Browser input http://127.0.0.1:8080/rumenz/index1, return 1:::hello

@RestController
@RequestMapping("/rumenz")
public class RumenzController {


     @Autowired
     RumenzConfig rumenzConfig;


     @RequestMapping("/index1")
     public String index1(){
          return rumenzConfig.getId()+":::"+rumenzConfig.getName();
     }
}

Parameter reference

stay application.properties The parameters in can be directly referenced to use

com.rumenz.id=1
com.rumenz.name=hello
com.rumenz.des=${com.rumenz.name}${com.rumenz.id}

newly build RumenzController test

@RestController
@RequestMapping("/rumenz")
public class RumenzController {

     @Value("${com.rumenz.des}")
     private String rumenzDes;

     @RequestMapping("/index2")
     public String index2(){
          // Profile parameter reference 
          //com.rumenz.des=${com.rumenz.name}${com.rumenz.id}
          return rumenzDes;
     }
}

Browser access http://127.0.0.1:8080/rumenz/index2 return hello1

Custom profile

General configuration items are application.properties in , Many times, we want to store some configuration information separately . For example, we created a rumenz.properties Custom profile for .

com.rumenz.tag= Entry station 
com.rumenz.txt= This is a tutorial website 

The custom configuration file system will not load automatically , Need to use @PropertySource load . You need to load multiple arrays that can be set .

test

@RestController
@RequestMapping("/rumenz")
@PropertySource(value="classpath:rumenz.properties",encoding = "utf-8")
public class Rumenz1Controller {

    @Value("${com.rumenz.tag}")
    private String rumenzTag;

    @Value("${com.rumenz.txt}")
    private String rumenzTxt;

    @RequestMapping("/index3")
    public String index3(){
        // This is the property configured in the custom configuration 
        return rumenzTag+rumenzTxt;
    }
}

Browser access http://127.0.0.1:8080/rumenz/index3, return This is a tutorial site

Load multiple custom profiles

@PropertySource(value = {
        "classpath:rumenz.properties",
        "classpath:rumenz2.properties",
}, encoding = "utf-8")

The source code address of this summary :

Introduce

原网站

版权声明
本文为[Introductory notes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201300930548235.html