默认的配置文件application.properties
# 自定义实体类,映射
dog.name=小狗
dog.age=12
自定义配置文件person.properties
# 自定义实体类,映射
# ${random.uuid} 随机的uuid
person.lastName=zhangsan${random.uuid}
# ${random.int(20)} 20以内的随机整数
person.age=${random.int(20)}
person.boss=false
person.birth=2017/12/12
# ${random.int[1024,65536]} 范围之内的随机整数
person.maps.k1=${random.int[1024,65536]}
person.maps.k2=${random.int[1024,65536]}
# ${person.lastName:name} 获取之前配置的值,没有则使用默认值,下例${person.hello:hello}=hello ${person.lastName:name}=zhangsanuuid
person.lists=cat,${person.hello:hello}_dog,${person.lastName:name}_pig
Dog实体类
1 package com.vegeta.spring_boot.entities;
2
3 import org.springframework.beans.factory.annotation.Value;
4 import org.springframework.stereotype.Component;
5
6 @Component
7 public class Dog {
8 @Value("${dog.name}")
9 private String name;
10 @Value("${dog.age}")
11 private Integer age;
12
13 public String getName() {
14 return name;
15 }
16
17 public void setName(String name) {
18 this.name = name;
19 }
20
21 public Integer getAge() {
22 return age;
23 }
24
25 public void setAge(Integer age) {
26 this.age = age;
27 }
28
29 @Override
30 public String toString() {
31 return "Dog{" +
32 "name='" + name + ''' +
33 ", age=" + age +
34 '}';
35 }
36 }
Person实体类
package com.vegeta.spring_boot.entities;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @PropertySource("classpath:person.properties")
* 指定加载配置文件的位置
*
* @ConfigurationProperties
* 告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
* prefix = "person":配置文件中哪个下面的所有属性进行一一映射
* 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
*/
@Component
@PropertySource("classpath:person.properties")
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map maps;
private List lists;
@Resource
private Dog dog;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getBoss() {
return boss;
}
public void setBoss(Boolean boss) {
this.boss = boss;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map getMaps() {
return maps;
}
public void setMaps(Map maps) {
this.maps = maps;
}
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + ''' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
测试类
1 package com.vegeta.spring_boot.controller;
2
3 import com.vegeta.spring_boot.entities.Person;
4 import org.junit.Test;
5 import org.junit.runner.RunWith;
6 import org.springframework.boot.test.context.SpringBootTest;
7 import org.springframework.test.context.junit4.SpringRunner;
8
9 import javax.annotation.Resource;
10
11 @RunWith(SpringRunner.class)
12 @SpringBootTest
13 public class EmployeeControllerTest {
14 @Resource
15 private Person person;
16
17 @Test
18 public void test() {
19 System.out.println(person);
20 }
21
22 }
打印结果
Person{lastName='zhangsan0b7728ee-86e1-4f6f-8302-89da57abc6a1', age=3, boss=false, birth=Tue Dec 12 00:00:00 CST 2017, maps={k2=2546, k1=63007}, lists=[cat, hello_dog, zhangsan561e37e8-28f2-4183-9fcd-f2216c5edd07_pig], dog=Dog{name='小狗', age=12}}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
1Tomcat的相关概念: Tomcat是Apache软件基金会一个核心项目,是一个开源免费的轻量级Web服务器,支持Servlet/JSP少量JavaEE规范。 概念中提到了JavaEE规范,那什么又是JavaEE规范呢?JavaEE: Java Enter…