Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
93 changes: 37 additions & 56 deletions README.md

Large diffs are not rendered by default.

27 changes: 13 additions & 14 deletions codes/core/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
# Spring 核心特性示例源码
# Spring 核心篇示例源码

> IoC container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP.

- [spring-boot-actuator](https://github.com/dunwu/spring-boot-tutorial/tree/master/codes/core/spring-boot-actuator) - 展示 Spring Boot 应用监控。
- [spring-boot-aop](https://github.com/dunwu/spring-boot-tutorial/tree/master/codes/core/spring-boot-aop) - 展示 Spring Boot 中应用 AOP 编程。
- [spring-boot-async](https://github.com/dunwu/spring-boot-tutorial/tree/master/codes/core/spring-boot-async) - 展示在 Spring Boot 中使用异步接口。
- [spring-boot-banner](https://github.com/dunwu/spring-boot-tutorial/tree/master/codes/core/spring-boot-banner) - 展示在 Spring Boot 中定制启动时的输出 Logo。
- [spring-boot-bean](https://github.com/dunwu/spring-boot-tutorial/tree/master/codes/core/spring-boot-bean) - 展示 Spring Boot 中应用 Lombok,简化大量的 getter、setter 、toString 等模板化代码;以及应用 Dozer 来简化大量的 JavaBean 互相转换代码。
- [spring-boot-profile](https://github.com/dunwu/spring-boot-tutorial/tree/master/codes/core/spring-boot-profile) - 展示 Spring Boot 中根据 profile 使程序在不同的环境下执行不同的行为。
- [spring-boot-property](https://github.com/dunwu/spring-boot-tutorial/tree/master/codes/core/spring-boot-property) - 全方位的演示 Spring Boot 加载属性的方式:记载 `properties` 和 `yaml` 两种文件;通过 `@Value`、`@ConfigurationProperties`、`Environment` 读取属性。

## 示例

- **`io.github.dunwu.spring.ioc`** spring 的依赖注入范例。
- **`io.github.dunwu.spring.aop`** spring 的切面编程范例。
- **`io.github.dunwu.spring.validator`** spring 的校验器范例。自定义一个简单的校验器。
- [spring-core-actuator](https://github.com/dunwu/spring-tutorial/tree/master/codes/core/actuator) - Spring 应用监控示例。
- [spring-core-aop](https://github.com/dunwu/spring-tutorial/tree/master/codes/core/aop) - Spring AOP 编程示例。
- [spring-core-async](https://github.com/dunwu/spring-tutorial/tree/master/codes/core/async) - Spring 使用异步接口示例。
- [spring-core-banner](https://github.com/dunwu/spring-tutorial/tree/master/codes/core/banner) - Spring 定制启动时的输出 Logo。
- [spring-core-bean](https://github.com/dunwu/spring-tutorial/tree/master/codes/core/bean) - Spring 管理 JavaBean 生命周期示例。
- [spring-core-conversion](https://github.com/dunwu/spring-tutorial/tree/master/codes/core/conversion) - Spring 数据转换示例。
- [spring-core-data-binding](https://github.com/dunwu/spring-tutorial/tree/master/codes/core/data-binding) - Spring 数据绑定示例。
- [spring-core-ioc](https://github.com/dunwu/spring-tutorial/tree/master/codes/core/ioc) - Spring IOC 示例。
- [spring-core-profile](https://github.com/dunwu/spring-tutorial/tree/master/codes/core/profile) - 在 Spring 中根据 profile 在不同的环境下执行不同的行为。
- [spring-core-property](https://github.com/dunwu/spring-tutorial/tree/master/codes/core/property) - 全方位的演示 Spring 加载属性的方式:记载 `properties` 和 `yaml` 两种文件;通过 `@Value`、`@ConfigurationProperties`、`Environment` 读取属性。
- [spring-core-resource](https://github.com/dunwu/spring-tutorial/tree/master/codes/core/resource) - Spring 资源加载示例。
- [spring-core-validation](https://github.com/dunwu/spring-tutorial/tree/master/codes/core/validation) - Spring 数据校验示例。
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,44 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<version>2.7.7</version>
</parent>

<groupId>io.github.dunwu.springboot</groupId>
<artifactId>spring-boot-property</artifactId>
<artifactId>spring-core-actuator</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Spring::核心::Actuator</name>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.github.dunwu.tool</groupId>
<artifactId>dunwu-tool-data</artifactId>
<version>1.1.0</version>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.github.dunwu.springboot.core;
package example.spring.core.actuator;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootActuatorApplication {
public class ActuatorApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootActuatorApplication.class, args);
SpringApplication.run(ActuatorApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.springboot.core;
package example.spring.core.actuator;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</encoder>
</appender>

<logger name="io.github.dunwu.springboot" level="INFO" />
<logger name="example.spring" level="INFO" />

<root level="WARN">
<appender-ref ref="CONSOLE" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.springboot.core;
package example.spring.core.actuator;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package io.github.dunwu.springboot.core;
package example.spring.core.actuator;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.util.Collections;
import java.util.Map;
Expand Down
19 changes: 4 additions & 15 deletions codes/core/spring-boot-aop/pom.xml → codes/core/aop/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<version>2.7.7</version>
</parent>

<groupId>io.github.dunwu.springboot</groupId>
<artifactId>spring-boot-aop</artifactId>
<artifactId>spring-core-aop</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Spring::核心::Aop</name>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
Expand Down Expand Up @@ -46,13 +44,4 @@
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.dunwu.springboot.core;
package example.spring.core.aop;

import io.github.dunwu.springboot.core.service.WelcomeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.springboot.core.aop;
package example.spring.core.aop;

import lombok.Data;
import lombok.experimental.Accessors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.springboot.core.aop;
package example.spring.core.aop;

import org.springframework.jdbc.core.JdbcTemplate;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.springboot.core.aop;
package example.spring.core.aop;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.springboot.core.aop;
package example.spring.core.aop;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.springboot.core.aop;
package example.spring.core.aop;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONUtil;
Expand Down Expand Up @@ -39,7 +39,7 @@ public MethodLogAspect(LogRecordDao logRecordDao) {
/**
* AOP 切点
*/
@Pointcut("@annotation(io.github.dunwu.springboot.core.aop.MethodLog)")
@Pointcut("@annotation(example.spring.core.aop.MethodLog)")
public void pointcut() { }

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package io.github.dunwu.springboot.core.aop;
package example.spring.core.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
Expand All @@ -17,7 +22,7 @@ public class ServiceAspect {

private final Logger log = LoggerFactory.getLogger(this.getClass());

@Pointcut("execution(* io.github.dunwu.springboot..*Service.*(..))")
@Pointcut("execution(* example.spring.core.aop..*Service.*(..))")
public void execute() {}

@Before("execute()")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.dunwu.springboot.core.service;
package example.spring.core.aop;

import io.github.dunwu.springboot.core.aop.MethodLog;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package io.github.dunwu.spring.core.aop;
package example.spring.core.aop.entity;

import org.aspectj.lang.annotation.*;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class Audience {

@Pointcut("execution(* io.github.dunwu.spring.core.aop.Instrumentalist.perform(..))")
@Pointcut("execution(* example.spring.core.aop.entity.Instrumentalist.perform(..))")
public void performance() {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.spring.core.aop;
package example.spring.core.aop.entity;

import org.springframework.stereotype.Component;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.spring.core.aop;
package example.spring.core.aop.entity;

public interface Performer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.datasource.username = root
spring.datasource.password = root
# 强制每次启动使用 sql 初始化数据,本项目仅为了演示方便,真实环境应避免这种模式
spring.datasource.initialization-mode = ALWAYS
spring.datasource.schema = classpath:sql/schema.sql
spring.sql.init.mode = always
spring.sql.init.schema-locations = classpath:sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</encoder>
</appender>

<logger name="io.github.dunwu.springboot" level="INFO" />
<logger name="example.spring" level="INFO" />

<root level="WARN">
<appender-ref ref="CONSOLE" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- 设置后,将查找使用@Component(@Controller是它的一种具体化)注解的类并将其注册为Bean -->
<context:component-scan base-package="io.github.dunwu" />
<context:component-scan base-package="example.spring.core.aop.entity" />

<!-- 启用aspectj aop -->
<aop:aspectj-autoproxy proxy-target-class="true" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.dunwu.springboot.core;
package example.spring.core.aop;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -15,11 +15,11 @@ public void testCommandLineOverrides(CapturedOutput capturedOutput) {
AopApplication.main(new String[] { "--author.name=Spring Boot" });
String output = capturedOutput.getOut();
assertThat(output).contains(
"call @Before, joinPoint: execution(String io.github.dunwu.springboot.core.service.WelcomeService.getMessage())");
"call @Before, joinPoint: execution(String example.spring.core.aop.WelcomeService.getMessage())");
assertThat(output).contains(
"call @After, joinPoint: execution(String io.github.dunwu.springboot.core.service.WelcomeService.getMessage())");
"call @After, joinPoint: execution(String example.spring.core.aop.WelcomeService.getMessage())");
assertThat(output).contains(
"call @AfterReturning, joinPoint: execution(String io.github.dunwu.springboot.core.service.WelcomeService.getMessage())");
"call @AfterReturning, joinPoint: execution(String example.spring.core.aop.WelcomeService.getMessage())");
assertThat(output).contains("Welcome Spring Boot");
}

Expand All @@ -28,11 +28,11 @@ public void testDefaultSettings(CapturedOutput capturedOutput) {
AopApplication.main(new String[0]);
String output = capturedOutput.getOut();
assertThat(output).contains(
"call @Before, joinPoint: execution(String io.github.dunwu.springboot.core.service.WelcomeService.getMessage())");
"call @Before, joinPoint: execution(String example.spring.core.aop.WelcomeService.getMessage())");
assertThat(output).contains(
"call @After, joinPoint: execution(String io.github.dunwu.springboot.core.service.WelcomeService.getMessage())");
"call @After, joinPoint: execution(String example.spring.core.aop.WelcomeService.getMessage())");
assertThat(output).contains(
"call @AfterReturning, joinPoint: execution(String io.github.dunwu.springboot.core.service.WelcomeService.getMessage())");
"call @AfterReturning, joinPoint: execution(String example.spring.core.aop.WelcomeService.getMessage())");
assertThat(output).contains("Welcome Zhang Peng");
}

Expand All @@ -46,11 +46,11 @@ public void testFailture(CapturedOutput capturedOutput) {

String output = capturedOutput.getOut();
assertThat(output).contains(
"call @Before, joinPoint: execution(String io.github.dunwu.springboot.core.service.WelcomeService.getMessage())");
"call @Before, joinPoint: execution(String example.spring.core.aop.WelcomeService.getMessage())");
assertThat(output).contains(
"call @After, joinPoint: execution(String io.github.dunwu.springboot.core.service.WelcomeService.getMessage())");
"call @After, joinPoint: execution(String example.spring.core.aop.WelcomeService.getMessage())");
assertThat(output).contains(
"call @AfterThrowing, joinPoint: execution(void io.github.dunwu.springboot.core.service.WelcomeService.getException())");
"call @AfterThrowing, joinPoint: execution(void example.spring.core.aop.WelcomeService.getException())");
assertThat(output).contains("Welcome Zhang Peng");
}

Expand Down
Loading