Skip to content

Commit dadcb9b

Browse files
committed
springboot-log 日志框架
1 parent 0d96135 commit dadcb9b

18 files changed

+672
-0
lines changed

springboot-logback/.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-logback/pom.xml

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
<groupId>net.codingme</groupId>
6+
<artifactId>springboot-logback</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
<name>springboot-logback</name>
10+
<description>日志框架的使用</description>
11+
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>2.1.1.RELEASE</version>
16+
<relativePath/> <!-- lookup parent from repository -->
17+
</parent>
18+
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22+
<java.version>1.8</java.version>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-test</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
<!-- Lombok 工具 -->
38+
<dependency>
39+
<groupId>org.projectlombok</groupId>
40+
<artifactId>lombok</artifactId>
41+
<optional>true</optional>
42+
</dependency>
43+
44+
<!-- 导入配置文件处理器,在配置springboot相关文件时候会有提示 -->
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-configuration-processor</artifactId>
48+
<optional>true</optional>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.junit.jupiter</groupId>
52+
<artifactId>junit-jupiter-api</artifactId>
53+
<version>RELEASE</version>
54+
<scope>compile</scope>
55+
</dependency>
56+
57+
<!-- 切换日志框架为 log4j2 -->
58+
<!--<dependency>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-starter-web</artifactId>
61+
<exclusions>
62+
<exclusion>
63+
<artifactId>spring-boot-starter-logging</artifactId>
64+
<groupId>org.springframework.boot</groupId>
65+
</exclusion>
66+
</exclusions>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>org.springframework.boot</groupId>
71+
<artifactId>spring-boot-starter-log4j2</artifactId>
72+
</dependency>-->
73+
74+
</dependencies>
75+
76+
<build>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.springframework.boot</groupId>
80+
<artifactId>spring-boot-maven-plugin</artifactId>
81+
</plugin>
82+
</plugins>
83+
</build>
84+
85+
86+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package net.codingme.boot;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.boot.CommandLineRunner;
7+
import org.springframework.boot.SpringApplication;
8+
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.context.ApplicationContext;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.ImportResource;
12+
import org.springframework.context.annotation.PropertySource;
13+
14+
import java.util.Arrays;
15+
16+
/**
17+
* <p>
18+
* 用于引入传统的 XML 配置文件
19+
*
20+
* @ImportResource(value = "classpath:spring-service.xml")
21+
* @Author niujinpeng
22+
* @Date 2018/12/7 0:04
23+
*/
24+
25+
//@ImportResource(value = "classpath:config/spring-service.xml")
26+
@SpringBootApplication
27+
public class BootApplication {
28+
29+
public static void main(String[] args) {
30+
SpringApplication.run(BootApplication.class, args);
31+
}
32+
33+
@Bean
34+
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
35+
return args -> {
36+
// 开始检查spring boot 提供的 beans
37+
System.out.println("Let's inspect the beans provided by Spring Boot:");
38+
String[] beanNames = ctx.getBeanDefinitionNames();
39+
Arrays.sort(beanNames);
40+
for (String beanName : beanNames) {
41+
//System.out.println(beanName);
42+
}
43+
};
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package net.codingme.boot.config;
2+
3+
import net.codingme.boot.service.HelloService;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
/**
8+
* <p>
9+
* 配置类,相当于传统Spring 开发中的 xml-> bean的配置
10+
*
11+
* @Author niujinpeng
12+
* @Date 2018/12/7 0:04
13+
*/
14+
@Configuration
15+
public class ServiceConfig {
16+
17+
/**
18+
* 默认添加到容器中的 ID 为方法名(helloService)
19+
*
20+
* @return
21+
*/
22+
@Bean
23+
public HelloService helloService() {
24+
return new HelloService();
25+
}
26+
27+
}
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;
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;
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;
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:config/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;
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+
}

0 commit comments

Comments
 (0)