Skip to content

Commit ad6e034

Browse files
committed
springboot-properties 配置文件
1 parent dadcb9b commit ad6e034

File tree

13 files changed

+471
-0
lines changed

13 files changed

+471
-0
lines changed

springboot-properties/.gitignore

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/target/
2+
/.mvn/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/build/
23+
/nbbuild/
24+
/dist/
25+
/nbdist/
26+
/.nb-gradle/

springboot-properties/pom.xml

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.1.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>net.codingme</groupId>
12+
<artifactId>springboot-properties</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<packaging>jar</packaging>
15+
<name>springboot-properties</name>
16+
<description>springboot-properties</description>
17+
<properties>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
20+
<java.version>1.8</java.version>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-test</artifactId>
32+
<scope>test</scope>
33+
</dependency>
34+
35+
<!-- Lombok 工具 -->
36+
<dependency>
37+
<groupId>org.projectlombok</groupId>
38+
<artifactId>lombok</artifactId>
39+
<optional>true</optional>
40+
</dependency>
41+
42+
<!-- 导入配置文件处理器,在配置springboot相关文件时候会有提示 -->
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-configuration-processor</artifactId>
46+
<optional>true</optional>
47+
</dependency>
48+
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-maven-plugin</artifactId>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package net.codingme.boot;
2+
3+
import org.springframework.boot.CommandLineRunner;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.ApplicationContext;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.ImportResource;
9+
import org.springframework.context.annotation.PropertySource;
10+
11+
import java.util.Arrays;
12+
13+
/**
14+
* <p>
15+
* 配置类,相当于传统Spring 开发中的 xml-> bean的配置
16+
*
17+
* @Author niujinpeng
18+
* @Date 2018/12/7 0:04
19+
*/
20+
@SpringBootApplication
21+
public class BootApplication {
22+
23+
public static void main(String[] args) {
24+
SpringApplication.run(BootApplication.class, args);
25+
}
26+
27+
@Bean
28+
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
29+
return args -> {
30+
// 开始检查spring boot 提供的 beans
31+
System.out.println("Let's inspect the beans provided by Spring Boot:");
32+
String[] beanNames = ctx.getBeanDefinitionNames();
33+
Arrays.sort(beanNames);
34+
for (String beanName : beanNames) {
35+
//System.out.println(beanName);
36+
}
37+
};
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package net.codingme.boot.controller;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
/**
10+
* <p>
11+
*
12+
*
13+
* @Author niujinpeng
14+
* @Date 2018/12/4 14:41
15+
*/
16+
@Slf4j
17+
@RestController
18+
public class HelloController {
19+
20+
@Value("${bootapp.description}")
21+
private String description;
22+
23+
@RequestMapping("/")
24+
public String index() {
25+
log.info(description);
26+
return "Greetings from Spring Boot!";
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.codingme.boot.domain.proper;
2+
3+
import lombok.Data;
4+
import lombok.Getter;
5+
import lombok.Setter;
6+
import org.springframework.boot.context.properties.ConfigurationProperties;
7+
import org.springframework.stereotype.Component;
8+
9+
/**
10+
* <p>
11+
*
12+
* @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法
13+
* @Component 加到spring 容器中
14+
* @ConfigurationProperties 告诉这个类的属性都是配置文件里的属性,prefix指定读取配置文件的前缀
15+
* @Author niujinpeng
16+
* @Date 2018/12/6 22:56
17+
*/
18+
19+
@Data
20+
@Component
21+
@ConfigurationProperties(prefix = "person.dog")
22+
public class Dog {
23+
private String name;
24+
private Integer age;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package net.codingme.boot.domain.proper;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.validation.annotation.Validated;
7+
8+
import javax.validation.constraints.Email;
9+
import java.util.Date;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
/**
14+
* <p>
15+
*
16+
* @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法
17+
* @Component 加到spring 容器中
18+
* @ConfigurationProperties 告诉这个类的属性都是配置文件里的属性,prefix指定读取配置文件的前缀
19+
* @Author niujinpeng
20+
* @Date 2018/12/6 22:54
21+
*/
22+
23+
@Data
24+
@Component
25+
@ConfigurationProperties(prefix = "person")
26+
@Validated
27+
public class Person {
28+
29+
private String lastName;
30+
private Integer age;
31+
private Date birth;
32+
private Map<String, String> maps;
33+
private List<String> lists;
34+
private Dog dog;
35+
36+
/**
37+
* 支持数据校验
38+
*/
39+
@Email
40+
private String email;
41+
42+
}
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package net.codingme.boot.domain.proper;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.context.annotation.PropertySource;
6+
import org.springframework.stereotype.Component;
7+
import org.springframework.validation.annotation.Validated;
8+
9+
import javax.validation.constraints.Email;
10+
import java.util.Date;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
/**
15+
* <p>
16+
* 使用@PropertySource加载自定义的配置文件,需要注意的是,由于@PropertySource指定的文件会优先加载,
17+
* 所以如果在 applocation.properties中存在相同的属性配置,会覆盖前者的值。
18+
*
19+
* @@PropertySource 加载自定义的配置文件
20+
* @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法
21+
* @Component 加到spring 容器中
22+
* @ConfigurationProperties 告诉这个类的属性都是配置文件里的属性,prefix指定读取配置文件的前缀
23+
* @Author niujinpeng
24+
* @Date 2018/12/6 22:54
25+
*/
26+
27+
@Data
28+
@Component
29+
@Validated
30+
@PropertySource(value = "classpath:domain-person.properties")
31+
@ConfigurationProperties(value = "person")
32+
public class PersonSource {
33+
34+
private String lastName;
35+
private Integer age;
36+
private Date birth;
37+
private Map<String, String> maps;
38+
private List<String> lists;
39+
private Dog dog;
40+
41+
/**
42+
* 支持数据校验
43+
*/
44+
@Email
45+
private String email;
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package net.codingme.boot.domain.proper;
2+
3+
import lombok.Data;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.validation.annotation.Validated;
7+
8+
import javax.validation.constraints.Email;
9+
import java.util.Date;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
/**
14+
* <p>
15+
*
16+
* @Data lombok的注解,会为这个类所有属性添加 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法
17+
* @Component 加到spring 容器中
18+
* @Author niujinpeng
19+
* @Date 2018/12/6 23:21
20+
*/
21+
@Data
22+
@Component
23+
@Validated
24+
public class PersonValue {
25+
26+
/**
27+
* 直接从配置文件读取一个值
28+
*/
29+
@Value("${person.last-name}")
30+
private String lastName;
31+
32+
/**
33+
* 支持SpEL表达式
34+
*/
35+
@Value("#{11*4/2}")
36+
private Integer age;
37+
38+
@Value("${person.birth}")
39+
private Date birth;
40+
41+
/**
42+
* 不支持复杂类型
43+
*/
44+
private Map<String, String> maps;
45+
private List<String> lists;
46+
private Dog dog;
47+
48+
/**
49+
* 不支持数据校验
50+
*/
51+
@Email
52+
@Value("xxx@@@@")
53+
private String email;
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server.port=8081
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
################################################################
2+
# \u670D\u52A1\u542F\u52A8\u7AEF\u53E3\u53F7
3+
server.port=8080
4+
spring.profiles.active=prod
5+
################################################################
6+
# \u914D\u7F6E\u5C5E\u6027\u503C\uFF08\u4F7F\u7528IDE\u8FDB\u884C\u914D\u7F6E\u9700\u8981\u5904\u7406\u7F16\u7801\u95EE\u9898\uFF0C\u4E0D\u7136\u4E2D\u6587\u4F1A\u53D1\u9001\u4E71\u7801\u73B0\u8C61\uFF09
7+
person.last-name=\u5F20\u4E09
8+
person.age=18
9+
person.birth=2018/12/06
10+
person.email=[email protected]
11+
person.maps.key1=c
12+
person.maps.key2=java
13+
person.maps.key3=golang
14+
person.lists=a,b,c,d
15+
person.dog.name=\u65FA\u8D22
16+
person.dog.age=1
17+
################################################################
18+
# \u751F\u6210\u968F\u673A\u503C
19+
bootapp.secret=$ {random.value}
20+
bootapp.number=$ {random.int}
21+
bootapp.bignumber=$ {random.long}
22+
bootapp.uuid=$ {random.uuid}
23+
bootapp.number.less.than.ten=$ {random.int\uFF0810\uFF09}
24+
bootapp.number.in.range=$ {random.int [1024,65536]}
25+
# \u5C5E\u6027\u7684\u5360\u4F4D\u7B26
26+
bootapp.name=SpringBoot
27+
bootapp.description=${bootapp.name}\u662F\u4E00\u4E2Aspring\u5E94\u7528\u7A0B\u5E8F

0 commit comments

Comments
 (0)