在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个starter都有默认配置,而这些默认配置足以满足正常的功能开发。
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
以上两个例子,说明了如需修改starter模块中的默认配置,只需要在在application.properties 添加需要修改的配置即可。
com.sam.name=sam com.sam.age=11 com.sam.desc=magical sam
第一种:使用spring支持的@Value()加载
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
}第二种:使用@ConfigurationProperties(prefix="") 设置前缀,属性上不需要添加注解。
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}实际开发中可能会有不同的环境,有开发环境、测试环境、生成环境。对于每个环境相关配置都可能有所不同,如:数据库信息、端口配置、本地路径配置等。
application-dev.properties //开发环境的配置文件 application-test.properties //测试环境的配置文件 application-prod.properties //生产环境的配置文件
spring.profiles.active=dev #引用测试的配置文件 #spring.profiles.active=test #引用生产的配置文件 #spring.profiles.active=prod
用命令运行jar包启动应用的时候,可以指定相应的配置.
java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
附:配置方式和优先级
这些方式优先级如下:
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以上就是Spring Boot系列之属性配置以及自定义属性配置的具体介绍的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号