Springboot (c): Springboot attribute profile detailed

Source: Internet
Author: User
Tags bind

The Springboot profile defaults to application.properties, but this section focuses on the Yaml file configuration, because the trend now is to use YAML, which is a data-description language similar to a subset of standard generic Markup language XML, and syntax is much simpler than XML. one, custom properties and loading

We changed the configuration file Application.properties in the previous project to Application.yml

Test:
  User:
    username:zhangsan
    age:18
    tostring:the age of ${test.user.username} is ${test.user.age}

Attribute Class

Package Cn.saytime.bean;

Import Org.springframework.beans.factory.annotation.Value;
Import org.springframework.stereotype.Component;

@Component public
class Propertiesconfig {

	@Value ("${test.user.username}")
	private String username;

	@Value ("${test.user.age}")
	private String age;
	
	@Value ("${test.user.tostring}")
	private String toString;
	
	// ... Getter Setter

}

Test controller

Package cn.saytime.web;

Import Cn.saytime.bean.PropertiesConfig;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.RestController;

@RestController public
class TestController {

	@Autowired
	private propertiesconfig propertiesconfig;

	@RequestMapping (value = "Test", method = requestmethod.get) public
	String test () {
//		return Propertiesconfig.getusername () + ":" + propertiesconfig.getage ();
		return propertiesconfig.gettostring ();
	}
}

Visit http://127.0.0.1/test

two, custom attribute injection bean

Inject the test.user from the Application.yml file above into the user object, note that the prefix specified here is test.user, the structure in the corresponding configuration file

@Component
@ConfigurationProperties (prefix = "test.user") public
class User {

	private String username;

	private int age;

	Public String GetUserName () {
		return username;
	}

	Getter Setter
}
@RestController public
class TestController {

	@Autowired
	private user user;

	@RequestMapping (value = "Test2", method = requestmethod.get) public
	String test2 () {
		return user.getusername ( ) + ":" + user.getage ();
	}
}

Access Http://localhost:8080/test2 ==> zhangsan:18 Third, custom additional configuration files

For example, we do not want to configure some configurations in Application.yml/properties. Create a new configuration file. Here, for example, create a new test.yml file. The configuration is the same as the application.yml above. So we should write this when we inject the object.

@Configuration
@PropertySource (value = "Classpath:test.yml")
@ConfigurationProperties (prefix = "Test.user") )
Four, multiple environment configuration files

In a real-world development environment, we need a different configuration environment, in the format application-{profile}.properties, where {profile} corresponds to your environment identity, such as: Application-test.properties: Test Environment Application-dev.properties: Development environment application-prod.properties: production environment

How to use. We only need to add in the APPLICATION.YML:

Spring:
  Profiles:
    Active:dev

Because the main portal is APPLICATION.YML, this specifies that the configuration file is dev, that is, enable the Application-dev.yml file

Among them application-dev.yml:

Server:
  port:8888

Starting the project, the discovery port of the program is no longer 8080, but 8888, indicating that the development environment is configured successfully. VI. Official Support default profile properties

http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/#common-application-properties Seven, Property Load Priority

1. @TestPropertySource Note
2. Command line parameter
3. Java System Properties (System.getproperties ())
4. Operating system environment Variables
5. Only the attributes contained in random.* produce a randomvaluepropertysource
6. Application configuration file (Application-{profile}.properties, including YAML and profile variables) outside the packaged Jar
7. Application configuration file within packaged jar (application-{ Profile}.properties, including YAML and profile variables)
8. Note 9 on @propertysource on the @configuration class.
Default properties (specified with springapplication.setdefaultproperties)

That is to say, I configure a file to configure a Name=zhangsan, and then the project into a jar, when running, if we use Java-jar app.jar--name= "Spring", then injected into the spring, the priority eight high , configuration file Priority

View Springboot official documentation to discover

Translation: A/config subdirectory under the current directory current directory a classpath under the/config package classpath root path (root)

This means: if we have multiple profiles, such as Src/main/resource/application.yml

Test:
  User:
    username:zhangsan
    age:18
    tostring:the age of ${test.user.username} is ${test.user.age}< C4/>name:springboot-root

test2: ${test1}-root

test3:springcloud-root

server:
  port:8080

Src/main/resource/config/application.yml

Test:
  User:
    username:lisi-config

name:springboot-config

test1: ${name}-config

test4: ${ Test3}-config

server:
  port:9090

Depending on the priority, you can get the properties that can be loaded into the Springboot app:

Test:
  User:
    username:lisi
    age:18
    tostring:the age of Lisi are

name:springboot-config
test 1:springboot-config-config
test2:springboot-config-config-root
test3:springcloud-root
test4: Springcloud-root-config

server:
  port:9090

If you can get the result above, it means you already understand.

Note When there is a high priority profile that has the same properties as the low priority profile, when the priority high profile is not in conflict, the low-priority profile properties are also loaded, instead of loading only the high-priority profile properties. Nine, simple summary

1. Normal custom attribute, inject 2 with @value ("${xxx}"), inject object, use @configurationproperties (prefix= "Test.user")




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.