Spring Boot Core-External Configuration __ command line parameter configuration

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.