Spring boot can be configured externally using a properties file, a Yaml file, or a command-line argument. 1 command line parameter configuration
Spring boot can be run based on a jar package, and a jar package can be run with the following command:
Java-jar Xxx.jar
You can modify the Tomcate port by using the following command:
Java-jar Xx.jar--server.port=9090
2 General Property configuration
In a general spring environment, the way to inject values in a properties file is to indicate the location of the file by @propertysource. The value is injected through the @value.
In the spring boot environment: define attributes in Application.properties. The value is injected through the @value. 3 Type-safe configuration (based on properties)
Using @value This injection attribute is a bit cumbersome in actual development, and it needs to be injected multiple times if the attribute is used in more than one place.
Spring boot provides a type-safe configuration that associates the Properties property with a bean and its properties by @configurationproperties. actual combat New Spring Boot project added in Pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-configuration-processor</artifactid>
<optional>true</optional>
</ Dependency>
Adding a configuration to the Application.properties
Author.name=wyh
author.age=30
Type-Safe bean
Package com.chenfeng.xiaolyuh.properties;
Import org.springframework.boot.context.properties.ConfigurationProperties;
Import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties (prefix = "author")//prefix specifies the prefix of properties, public
class Authorsettings {
private String name;
Private long age;
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Public long Getage () {return age
;
}
public void Setage (long) {
this.age = age;
}
}
Validation code
Package com.chenfeng.xiaolyuh;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RestController;
Import com.chenfeng.xiaolyuh.properties.AuthorSettings;
@SpringBootApplication//This is the core annotation of the Spring boot project, mainly to turn on automatic configuration. @RestController public class Springbootstudentapplication {@Autowired//is injected directly into the configuration class via annotations private Authorsettings Authorsett
Ings; @RequestMapping ("/") public String Index () {return "Hello world!
"+ authorsettings.getname () +":: "+ authorsettings.getage (); }//Standard Java application Main method, primarily acting as entry-initiated portal public static void main (string[] args) {Springapplication.run (Springbootstudentap
Plication.class, args); }
}
Reference