Home >Java >javaTutorial >A detailed introduction to the property configuration and custom property configuration of the Spring Boot series
In the process of using spring boot, you can find that only a very small amount of configuration is needed in the project to complete the corresponding functions. This is due to the modular configuration in spring boot. Each Starter that is relied on in pom.xml has Default configurations are sufficient for normal function development.
server.port=8888
druid.url=jdbc:mysql://192.168.0.20:3306/test druid.driver-class=com.mysql.jdbc.Driver druid.username=root druid.password=123456 druid.initial-size=1 druid.min-idle=1 druid.max-active=20 druid.test-on-borrow=true
The above two examples illustrate If you need to modify the default configuration in the starter module, you only need to add the configuration that needs to be modified in application.properties.
com.sam.name=sam com.sam.age=11 com.sam.desc=magical sam
The first one: Use the @Value() supported by spring to load
package com.sam.demo.conf; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * @author sam * @since 2017/7/15 */ @Component public class Sam { //获取application.properties的属性 @Value("${com.sam.name}") private String name; @Value("${com.sam.age}") private int age; @Value("${com.sam.desc}") private String desc; //getter & setter }
The second one: Use @ConfigurationProperties(prefix="") to set the prefix, and no annotations are needed on the properties.
package com.sam.demo.conf; import org.springframework.stereotype.Component; /** * @author sam * @since 2017/7/15 */ @Component @ConfigurationProperties(prefix = "com.sam") public class Sam { private String name; private int age; private String desc; //getter & setter }
package com.sam.demo.controller; import com.sam.demo.conf.Sam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author sam * @since 2017/7/14 */ @RestController public class IndexController { @Autowired private Sam sam; @RequestMapping("/index") public String index() { System.out.println(sam.getName() + " " + sam.getAge() + " " + sam.getDesc()); return "index"; } }
com.sam.name=sam com.sam.age=11 com.sam.desc=${name} is ${age} years old.
#获取随机字符串 com.sam.randomValue=${random.value} #获取随机字符串:${random.value} #获取随机int:${random.int} #获取10以内的随机数:${random.int(10)} #获取10-20的随机数:${random.int[10,20]} #获取随机long:${random.long} #获取随机uuid:${random.uuid}
In actual development, there may be different environments, including development environment, test environment, and production environment. Related configurations may be different for each environment, such as database information, port configuration, local path configuration, etc.
application-dev.properties //开发环境的配置文件 application-test.properties //测试环境的配置文件 application-prod.properties //生产环境的配置文件
spring.profiles.active=dev #引用测试的配置文件 #spring.profiles.active=test #引用生产的配置文件 #spring.profiles.active=prod
When using the command to run the jar package to start the application, you can specify the corresponding configuration.
java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
Attachment: Configuration method and priority
这些方式优先级如下: a. 命令行参数 b. 来自java:comp/env的JNDI属性 c. Java系统属性(System.getProperties()) d. 操作系统环境变量 e. RandomValuePropertySource配置的random.*属性值 f. jar外部的application-{profile}.properties或application.yml(带spring.profile)配置文件 g. jar内部的application-{profile}.properties或application.yml(带spring.profile)配置文件 h. jar外部的application.properties或application.yml(不带spring.profile)配置文件 i. jar内部的application.properties或application.yml(不带spring.profile)配置文件 j. @Configuration注解类上的@PropertySource k. 通过SpringApplication.setDefaultProperties指定的默认属性
springApplication.setAddCommandLineProperties(false);
package com.sam.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { // SpringApplication.run(DemoApplication.class, args); SpringApplication springApplication = new SpringApplication(DemoApplication.class); //禁止命令行设置参数 springApplication.setAddCommandLineProperties(false); springApplication.run(args); } }
server: port: 9999 com: sam: name: sam age: 11 desc: magical sam
The above is the detailed content of A detailed introduction to the property configuration and custom property configuration of the Spring Boot series. For more information, please follow other related articles on the PHP Chinese website!