赞
踩
–file: ./config/–file: ./–classpath: /config/–classpath: /
翻译成语言如下( 按照优先级从高到低的顺序 ):
$ java -jar myproject.jar --spring .config .name = myproject
java -jar run-0.0.1-SNAPSHOT.jar --spring .config .location = D:/application.properties
spring.config.use-legacy-processing = true
server.port = 8081spring.datasource.driver-class-name = com.mysql.jdbc.Driverspring.config.name = application
- package com.lagou.pojo;
-
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
-
- import java.util.Arrays;
- import java.util.List;
- import java.util.Map;
-
- @Component
- @ConfigurationProperties(prefix = "person") //实现批量注入(set方法)
- public class Person {
-
- private int id; //id
- private String name; //名称
- private List hobby; //爱好
- private String[] family; //家庭成员
- private Map map;
- private Pet pet; //宠物
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public List getHobby() {
- return hobby;
- }
-
- public void setHobby(List hobby) {
- this.hobby = hobby;
- }
-
- public String[] getFamily() {
- return family;
- }
-
- public void setFamily(String[] family) {
- this.family = family;
- }
-
- public Map getMap() {
- return map;
- }
-
- public void setMap(Map map) {
- this.map = map;
- }
-
- public Pet getPet() {
- return pet;
- }
-
- public void setPet(Pet pet) {
- this.pet = pet;
- }
-
- @Override
- public String toString() {
- return "Person{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", hobby=" + hobby +
- ", family=" + Arrays.toString(family) +
- ", map=" + map +
- ", pet=" + pet +
- '}';
- }
- }
- package com.lagou.pojo;
-
- public class Pet {
-
- // 类型
- private String type;
- // 名称
- private String name;
-
- public String getType() {
- return type;
- }
-
- public void setType(String type) {
- this.type = type;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @Override
- public String toString() {
- return "Pet{" +
- "type='" + type + '\'' +
- ", name='" + name + '\'' +
- '}';
- }
- }
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
- </dependency>
- @RunWith(SpringRunner.class)
- @SpringBootTest
- class Springboot01DemoApplicationTests {
-
- @Autowired
- private Person person;
-
-
- @Test
- void contextLoads() {
- System.out.println(person);
- }
- }
yml 和 xml 相比,少了一些结构化的代码,使数据更直接,一目了然l 相比 properties 文件更简洁
YAML文件的扩展名可以使用.yml或者.yaml。
application.yml 文件使用 “key: (空格) value” 格式配置属性,使用缩进控制层级关系。
server :port : 8080servlet :context-path : /hello
person :hobby :- play- read- sleep
person :hobby :play,read,sleep
person :hobby : [ play , read , sleep ]
person :map :k1 : v1k2 : v2
person :map : { k1 : v1 , k2 : v2 }
person: id: 2 name: lucy hobby: [吃饭,睡觉,打王者] family: [father,mother] map: {k1: v1,k2: v2} pet: {type: pig,name: 佩奇}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。