当前位置:网站首页>03_ Spingboot core profile

03_ Spingboot core profile

2022-06-24 23:06:00 Book opens autumn maple

Spring Boot The core configuration file is used to configure Spring Boot Program , The name must be application Start

1.  Core configuration format

(1).properties file ( This file is used by default )

By modifying the application.properties The configuration file , After modifying the default tomcat Port number and file root of the project ( Site )

SpringBootController

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SpringBootController {
    @RequestMapping(value = "/springBoot/say")
    @ResponseBody
    public String say() {
        return "Hello,springBoot!";
    }
}

application.properties 

Key value pairs properties Property file configuration mode

# configure port 
server.port=9001

# Configure site name 
server.servlet.context-path=/001-springboot-first

Start the test --》 Page display results  localhost:9001/001-springboot-first/springBoot/say

(2).yml file

yml Document and properties There is no difference between files , It's just different configurations .

yml It's a kind of yaml Format of the configuration file , It mainly adopts certain Space 、 Line feed and other formatting configuration .

yaml It is an intuitive data serialization format that can be recognized by computer , It's easy for people to read ,yaml Be similar to xml, But grammar is better than xml Concise and many , Value must have a space with the previous colon configuration item , yml Suffixes can also be used yaml suffix

 SpringBootController

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SpringBootController {
    @RequestMapping(value = "/springBoot/say")
    @ResponseBody
    public String say() {
        return "Hello,springBoot!";
    }
}

application.yml​​​​​​​

server:
  port: 8001
  servlet:
    context-path: /001-springboot-first

Be careful : When two format configuration files exist at the same time , It uses .properties The configuration file , To demonstrate yml, You can rename it first , Rerun Application, Check the startup port and context root  

Start the test --》 Page display results  localhost:8001/001-springboot-first/springBoot/say

​​​​​​​2. Multi environment configuration

In the process of actual development , Our project will go through many stages ( Development --> test --> go online ), The configuration of each phase will also be different , for example : port 、 Up and down Wengen 、 Database etc. , At this time, in order to facilitate switching between different environments ,SpringBoot Provides multiple environment configurations , The specific steps are as follows

SpringBootController

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SpringBootController {
    @RequestMapping(value = "/springBoot/say")
    @ResponseBody
    public String say() {
        return "Hello,springBoot!";
    }
}

application.properties 

The value to the right of the equal sign is consistent with the environment ID name of the configuration file , You can change the configuration of the master configuration file , Rerun Application, View the boot port and context root directory

# Activate the environment configuration 
spring.profiles.active=dev

application-dev.properties 

# Development environment core configuration file 
server.port=7001
server.servlet.context-path=/001-springboot-first

application-product.properties 

# Production environment core configuration file 
server.port=6001
server.servlet.context-path=/001-springboot-first

application-test.properties 

# Test environment core configuration file 
server.port=5001
server.servlet.context-path=/001-springboot-first

Start the test --》 Page display results  localhost:7001/001-springboot-first/springBoot/say

​​​​​​​​​​​​​​3. Spring Boot Custom configuration

stay SpringBoot In the core configuration file of , In addition to using built-in configuration items , We can also customize the configuration , Then use the following annotation to read the configured attribute value

​​​​​​​(1)@Value annotation

In the core configuration file applicatin.properties in , Add two custom configuration items school.name and school.webSite. stay IDEA You can't see these two properties SpringBoot distinguish , The background is orange

stay SpringBootController Define properties in , And use @Value Annotations or custom configuration values , And test the method

SpringBootController 

@Controller
public class SpringBootController {

    @Value("${school.name}")
    private String schoolName;

    @Value("${school.webSite}")
    private String schoolWebsite;

    @RequestMapping(value = "/springBoot/say")
    @ResponseBody
    public String say() {
        return "Hello,springBoot!--->" + schoolName + "-->" + schoolWebsite;
    }
}

application.properties 

The value to the right of the equal sign is consistent with the environment ID name of the configuration file , You can change the configuration of the master configuration file , Rerun Application, View the boot port and context root directory

# Activate the environment configuration 
spring.profiles.active=dev

application-dev.properties 

# Development environment core configuration file 
server.port=7001
server.servlet.context-path=/001-springboot-first

Start the test --》 Page display results  localhost:7001/001-springboot-first/springBoot/say

(2)​​​​​​​​​​​​​​@ConfigurationProperties

ConfigInfo

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "school")
@Component
public class ConfigInfo {
    private String name;
    private String website;

    public ConfigInfo() {
    }

    public String getName() {
        return name;
    }

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

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    @Override
    public String toString() {
        return "ConfigInfo{" +
                "name='" + name + '\'' +
                ", website='" + website + '\'' +
                '}';
    }
}

SpringBootController 

@Controller
public class SpringBootController {

    @Autowired
    ConfigInfo configInfo;

    @Value("${school.name}")
    private String schoolName;

    @Value("${school.webSite}")
    private String schoolWebsite;

    @RequestMapping(value = "/springBoot/say")
    @ResponseBody
    public String say() {
        return "Hello,springBoot!--->" + schoolName + "-->" + schoolWebsite + "-->" + configInfo;
    }
}

application.properties 

The value to the right of the equal sign is consistent with the environment ID name of the configuration file , You can change the configuration of the master configuration file , Rerun Application, View the boot port and context root directory

# Activate the environment configuration 
spring.profiles.active=dev

application-dev.properties 

# Development environment core configuration file 
server.port=7001
server.servlet.context-path=/001-springboot-first

  Start the test --》 Page display results  localhost:7001/001-springboot-first/springBoot/say

ConfigInfo  Red explosion problem   solve

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
原网站

版权声明
本文为[Book opens autumn maple]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241714023566.html