diff --git a/01.Start-Spring-Boot/pom.xml b/01.Spring-Boot-Start/pom.xml
similarity index 79%
rename from 01.Start-Spring-Boot/pom.xml
rename to 01.Spring-Boot-Start/pom.xml
index e9fe5c98..aca509fa 100644
--- a/01.Start-Spring-Boot/pom.xml
+++ b/01.Spring-Boot-Start/pom.xml
@@ -4,24 +4,24 @@
4.0.0
com.springboot
- Start-Spring-Boot
+ Spring-Boot-Start
0.0.1-SNAPSHOT
jar
- Start-Spring-Boot
+ 01.Spring-Boot-Start
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
- 1.5.9.RELEASE
+ 2.3.4.RELEASE
UTF-8
UTF-8
- 1.7
+ 1.8
@@ -37,13 +37,7 @@
-
-
- nexus-aliyun
- Nexus aliyun
- http://maven.aliyun.com/nexus/content/groups/public
-
-
+
diff --git a/01.Start-Spring-Boot/src/main/java/com/springboot/demo/DemoApplication.java b/01.Spring-Boot-Start/src/main/java/com/springboot/demo/Application01.java
similarity index 83%
rename from 01.Start-Spring-Boot/src/main/java/com/springboot/demo/DemoApplication.java
rename to 01.Spring-Boot-Start/src/main/java/com/springboot/demo/Application01.java
index e584ba86..295712c4 100644
--- a/01.Start-Spring-Boot/src/main/java/com/springboot/demo/DemoApplication.java
+++ b/01.Spring-Boot-Start/src/main/java/com/springboot/demo/Application01.java
@@ -7,7 +7,7 @@
@RestController
@SpringBootApplication
-public class DemoApplication {
+public class Application01 {
@RequestMapping("/")
String index() {
@@ -15,6 +15,6 @@ String index() {
}
public static void main(String[] args) {
- SpringApplication.run(DemoApplication.class, args);
+ SpringApplication.run(Application01.class, args);
}
}
diff --git a/01.Start-Spring-Boot/src/main/resources/application.properties b/01.Spring-Boot-Start/src/main/resources/application.properties
similarity index 100%
rename from 01.Start-Spring-Boot/src/main/resources/application.properties
rename to 01.Spring-Boot-Start/src/main/resources/application.properties
diff --git a/01.Start-Spring-Boot/src/test/java/com/springboot/demo/DemoApplicationTests.java b/01.Spring-Boot-Start/src/test/java/com/springboot/demo/DemoApplicationTests.java
similarity index 100%
rename from 01.Start-Spring-Boot/src/test/java/com/springboot/demo/DemoApplicationTests.java
rename to 01.Spring-Boot-Start/src/test/java/com/springboot/demo/DemoApplicationTests.java
diff --git a/02.Spring-Boot-Config/pom.xml b/02.Spring-Boot-Config/pom.xml
index b84d7045..3335efa2 100644
--- a/02.Spring-Boot-Config/pom.xml
+++ b/02.Spring-Boot-Config/pom.xml
@@ -8,20 +8,20 @@
0.0.1-SNAPSHOT
jar
- Spring-Boot-Config
+ 02.Spring-Boot-Config
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
- 1.5.9.RELEASE
+ 2.3.4.RELEASE
UTF-8
UTF-8
- 1.7
+ 1.8
@@ -29,7 +29,13 @@
org.springframework.boot
spring-boot-starter-web
-
+
+
+ org.springframework.boot
+ spring-boot-configuration-processor
+ true
+
+
org.springframework.boot
spring-boot-starter-test
@@ -38,14 +44,6 @@
-
-
- nexus-aliyun
- Nexus aliyun
- http://maven.aliyun.com/nexus/content/groups/public
-
-
-
@@ -55,5 +53,4 @@
-
diff --git a/02.Spring-Boot-Config/readme02.md b/02.Spring-Boot-Config/readme02.md
new file mode 100644
index 00000000..ef88ee33
--- /dev/null
+++ b/02.Spring-Boot-Config/readme02.md
@@ -0,0 +1,224 @@
+## 定制Banner
+
+Spring Boot项目在启动的时候会有一个默认的启动图案:
+
+```
+ . ____ _ __ _ _
+ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
+( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
+ \\/ ___)| |_)| | | | | || (_| | ) ) ) )
+ ' |____| .__|_| |_|_| |_\__, | / / / /
+ =========|_|==============|___/=/_/_/_/
+ :: Spring Boot :: (v2.3.4.RELEASE)
+```
+
+我们可以把这个图案修改为自己想要的。在src/main/resources目录下新建banner.txt文件,然后将自己的图案黏贴进去即可。ASCII图案可通过网站http://www.network-science.de/ascii/一键生成,比如输入mrbird生成图案后复制到banner.txt,启动项目,控制台输出如下:
+
+```
+ _ _ _ _ _ _
+ / \ / \ / \ / \ / \ / \
+( s | p | r | i | n | g )
+ \_/ \_/ \_/ \_/ \_/ \_/
+……
+2022-01-27 22:20:59.267 INFO 22216 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
+2022-01-27 22:20:59.386 INFO 22216 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
+2022-01-27 22:20:59.393 INFO 22216 --- [ main] com.springboot.Application : Started Application in 1.486 seconds (JVM running for 2.941)
+```
+
+
+
+banner也可以关闭,在main方法中:
+
+```
+public static void main(String[] args) {
+ SpringApplication app = new SpringApplication(DemoApplication.class);
+ app.setBannerMode(Mode.OFF);
+ app.run(args);
+}
+```
+
+
+
+## 全局配置文件
+
+在src/main/resources目录下,Spring Boot提供了一个名为application.properties的全局配置文件,可对一些默认配置的配置值进行修改。
+
+> 附:[application.properties中可配置所有官方属性](https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html)
+
+### 自定义属性值
+
+Spring Boot允许我们在application.properties下自定义一些属性,比如:
+
+```
+mrbird.blog.name=mrbird's blog
+mrbird.blog.title=Spring Boot
+```
+
+
+
+定义一个BlogProperties Bean,通过`@Value("${属性名}")`来加载配置文件中的属性值:
+
+```
+@Component
+public class BlogProperties {
+
+ @Value("${mrbird.blog.name}")
+ private String name;
+
+ @Value("${mrbird.blog.title}")
+ private String title;
+
+ // get,set略
+}
+```
+
+
+
+编写IndexController,注入该Bean:
+
+```
+@RestController
+public class IndexController {
+ @Autowired
+ private BlogProperties blogProperties;
+
+ @RequestMapping("/")
+ String index() {
+ return blogProperties.getName()+"——"+blogProperties.getTitle();
+ }
+}
+```
+
+
+
+启动项目,访问[http://localhost:8080](http://localhost:8080/),页面显示如下:
+
+
+
+在属性非常多的情况下,也可以定义一个和配置文件对应的Bean:
+
+```
+@ConfigurationProperties(prefix="mrbird.blog")
+public class ConfigBean {
+ private String name;
+ private String title;
+ // get,set略
+}
+```
+
+
+
+通过注解`@ConfigurationProperties(prefix="mrbird.blog")`指明了属性的通用前缀,通用前缀加属性名和配置文件的属性名一一对应。
+
+除此之外还需在Spring Boot入口类加上注解`@EnableConfigurationProperties({ConfigBean.class})`来启用该配置:
+
+```
+@SpringBootApplication
+@EnableConfigurationProperties({ConfigBean.class})
+public class Application {
+
+ public static void main(String[] args) {
+ SpringApplication.run(Application.class, args);
+ }
+}
+```
+
+
+
+之后便可在IndexController中注入该Bean,并使用了:
+
+```
+@RestController
+public class IndexController {
+ @Autowired
+ private ConfigBean configBean;
+
+ @RequestMapping("/")
+ String index() {
+ return configBean.getName()+"——"+configBean.getTitle();
+ }
+}
+```
+
+
+
+### 属性间的引用
+
+在application.properties配置文件中,各个属性可以相互引用,如下:
+
+```
+mrbird.blog.name=mrbird's blog
+mrbird.blog.title=Spring Boot
+mrbird.blog.wholeTitle=${mrbird.blog.name}--${mrbird.blog.title}
+```
+
+
+
+## 自定义配置文件
+
+除了可以在application.properties里配置属性,我们还可以自定义一个配置文件。在src/main/resources目录下新建一个test.properties:
+
+```
+test.name=KangKang
+test.age=25
+```
+
+
+
+定义一个对应该配置文件的Bean:
+
+```
+@Configuration
+@ConfigurationProperties(prefix="test")
+@PropertySource("classpath:test.properties")
+@Component
+public class TestConfigBean {
+ private String name;
+ private int age;
+ // get,set略
+}
+```
+
+
+
+注解`@PropertySource("classpath:test.properties")`指明了使用哪个配置文件。要使用该配置Bean,同样也需要在入口类里使用注解`@EnableConfigurationProperties({TestConfigBean.class})`来启用该配置。
+
+## 通过命令行设置属性值
+
+在运行Spring Boot jar文件时,可以使用命令`java -jar xxx.jar --server.port=8081`来改变端口的值。这条命令等价于我们手动到application.properties中修改(如果没有这条属性的话就添加)server.port属性的值为8081。
+
+如果不想项目的配置被命令行修改,可以在入口文件的main方法中进行如下设置:
+
+```
+public static void main(String[] args) {
+ SpringApplication app = new SpringApplication(Application.class);
+ app.setAddCommandLineProperties(false);
+ app.run(args);
+}
+```
+
+
+
+## 使用xml配置
+
+虽然Spring Boot并不推荐我们继续使用xml配置,但如果出现不得不使用xml配置的情况,Spring Boot允许我们在入口类里通过注解`@ImportResource({"classpath:some-application.xml"})`来引入xml配置文件。
+
+## Profile配置
+
+Profile用来针对不同的环境下使用不同的配置文件,多环境配置文件必须以`application-{profile}.properties`的格式命,其中`{profile}`为环境标识。比如定义两个配置文件:
+
+- application-dev.properties:开发环境
+
+ ```
+ server.port=8080
+ ```
+
+- application-prod.properties:生产环境
+
+ ```
+ server.port=8081
+ ```
+
+至于哪个具体的配置文件会被加载,需要在application.properties文件中通过`spring.profiles.active`属性来设置,其值对应`{profile}`值。
+
+如:`spring.profiles.active=dev`就会加载application-dev.properties配置文件内容。可以在运行jar文件的时候使用命令`java -jar xxx.jar --spring.profiles.active={profile}`切换不同的环境配置。
\ No newline at end of file
diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/Application.java b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java
similarity index 55%
rename from 02.Spring-Boot-Config/src/main/java/com/springboot/Application.java
rename to 02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java
index eca23874..68637c6c 100644
--- a/02.Spring-Boot-Config/src/main/java/com/springboot/Application.java
+++ b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java
@@ -1,20 +1,20 @@
package com.springboot;
+import com.springboot.bean.PropertySourceGet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import com.springboot.bean.ConfigBean;
-import com.springboot.bean.TestConfigBean;
@SpringBootApplication
-@EnableConfigurationProperties({ConfigBean.class,TestConfigBean.class})
-//@ImportResource({"classpath:some-application.xml"})
-public class Application {
-
+@EnableConfigurationProperties({ConfigBean.class})
+//@EnableConfigurationProperties({ConfigBean.class, PropertySourceGet.class})
+public class Application02 {
public static void main(String[] args) {
- SpringApplication app = new SpringApplication(Application.class);
+ SpringApplication app = new SpringApplication(Application02.class);
app.setAddCommandLineProperties(false);
+// app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
}
diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean.java
index 2a57ee13..6f81ad4f 100644
--- a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean.java
+++ b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean.java
@@ -2,6 +2,13 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
+/**
+ * @author: wyq
+ * @create time: 2022/1/27
+ * @description:
+ * ProfileProperties类没有加@Component注解。
+ * 在要使用ProfileProperties的地方使用@EnableConfigurationProperties注册我们的配置 bean:
+ */
@ConfigurationProperties(prefix="mrbird.blog")
public class ConfigBean {
private String name;
@@ -24,6 +31,14 @@ public String getWholeTitle() {
}
public void setWholeTitle(String wholeTitle) {
this.wholeTitle = wholeTitle;
- }
-
+ }
+
+ @Override
+ public String toString() {
+ return "ConfigBean{" +
+ "name='" + name + '\'' +
+ ", title='" + title + '\'' +
+ ", wholeTitle='" + wholeTitle + '\'' +
+ '}';
+ }
}
diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean2.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean2.java
new file mode 100644
index 00000000..0cab0584
--- /dev/null
+++ b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean2.java
@@ -0,0 +1,45 @@
+package com.springboot.bean;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author: wyq
+ * @create time: 2022/1/27
+ * @description:
+ * 通过@ConfigurationProperties读取配置信息并与bean绑定。
+ */
+@Component
+@ConfigurationProperties(prefix="my.config.get")
+public class ConfigBean2 {
+ private String name;
+ private String title;
+ private String wholeTitle;
+ public String getName() {
+ return name;
+ }
+ public void setName(String name) {
+ this.name = name;
+ }
+ public String getTitle() {
+ return title;
+ }
+ public void setTitle(String title) {
+ this.title = title;
+ }
+ public String getWholeTitle() {
+ return wholeTitle;
+ }
+ public void setWholeTitle(String wholeTitle) {
+ this.wholeTitle = wholeTitle;
+ }
+
+ @Override
+ public String toString() {
+ return "ConfigBean{" +
+ "name='" + name + '\'' +
+ ", title='" + title + '\'' +
+ ", wholeTitle='" + wholeTitle + '\'' +
+ '}';
+ }
+}
diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet.java
new file mode 100644
index 00000000..9b0460a0
--- /dev/null
+++ b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet.java
@@ -0,0 +1,38 @@
+package com.springboot.bean;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author: wyq
+ * @create time: 2022/1/27
+ * @description: @PropertySource读取指定 properties文件
+ * @PropertySource不支持加载yml配置文件,所以需要自定义一个配置类
+ */
+@ConfigurationProperties(prefix = "test")
+@PropertySource("classpath:property.properties")
+//@PropertySource("classpath:property.yml") //此时获取name和age分别为null,0
+@Component
+public class PropertySourceGet {
+ private String name;
+ private int age;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+}
diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet2.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet2.java
new file mode 100644
index 00000000..85e6fa71
--- /dev/null
+++ b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet2.java
@@ -0,0 +1,44 @@
+package com.springboot.bean;
+
+import com.springboot.yml.MixPropertySourceFactory;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author: wyq
+ * @create time: 2022/1/27
+ * @description: @PropertySource读取指定 properties文件
+ * @PropertySource不支持加载yml配置文件,所以需要自定义一个配置类
+ */
+@ConfigurationProperties(prefix = "test2")
+@PropertySource(value = {"classpath:property.yml"}, factory = MixPropertySourceFactory.class)
+@Component
+public class PropertySourceGet2 {
+ private String name;
+ private int age;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+ @Override
+ public String toString() {
+ return "PropertySourceGet2{" +
+ "name='" + name + '\'' +
+ ", age=" + age +
+ '}';
+ }
+}
diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/TestConfigBean.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/TestConfigBean.java
deleted file mode 100644
index 46673053..00000000
--- a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/TestConfigBean.java
+++ /dev/null
@@ -1,28 +0,0 @@
-package com.springboot.bean;
-
-import org.springframework.boot.context.properties.ConfigurationProperties;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.context.annotation.PropertySource;
-import org.springframework.stereotype.Component;
-
-@Configuration
-@ConfigurationProperties(prefix="test")
-@PropertySource("classpath:test.properties")
-@Component
-public class TestConfigBean {
- private String name;
- private int age;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
-
-}
diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/BlogProperties.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ValueGetProperties.java
similarity index 61%
rename from 02.Spring-Boot-Config/src/main/java/com/springboot/bean/BlogProperties.java
rename to 02.Spring-Boot-Config/src/main/java/com/springboot/bean/ValueGetProperties.java
index 15694fc6..3add7bec 100644
--- a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/BlogProperties.java
+++ b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ValueGetProperties.java
@@ -3,8 +3,13 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
+/**
+ * @author: wyq
+ * @create time: 2022/1/27
+ * @description: 使用@Value("${property}")读取比较简单的配置信息
+ */
@Component
-public class BlogProperties {
+public class ValueGetProperties {
@Value("${mrbird.blog.name}")
private String name;
@@ -27,5 +32,12 @@ public String getTitle() {
public void setTitle(String title) {
this.title = title;
}
-
+
+ @Override
+ public String toString() {
+ return "BlogProperties{" +
+ "name='" + name + '\'' +
+ ", title='" + title + '\'' +
+ '}';
+ }
}
diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java b/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java
index 16063f01..6575ec5a 100644
--- a/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java
+++ b/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java
@@ -1,25 +1,50 @@
package com.springboot.controller;
+import com.springboot.bean.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
-import com.springboot.bean.BlogProperties;
-import com.springboot.bean.ConfigBean;
-import com.springboot.bean.TestConfigBean;
-
@RestController
+@RequestMapping("/v2")
public class IndexController {
- @Autowired
- private BlogProperties blogProperties;
- @Autowired
- private ConfigBean configBean;
- @Autowired
- private TestConfigBean testConfigBean;
-
- @RequestMapping("/")
- String index() {
- return testConfigBean.getName()+","+testConfigBean.getAge();
- }
+ @Autowired
+ private ValueGetProperties blogProperties;
+
+ @Autowired
+ private ConfigBean configBean;
+
+ @Autowired
+ private ConfigBean2 configBean2;
+
+ @Autowired
+ private PropertySourceGet testConfigBean;
+ @Autowired
+ private PropertySourceGet2 testConfigBean2;
+
+ @RequestMapping("/blog")
+ public String testBlog() {
+ return blogProperties.toString();
+ }
+
+ @RequestMapping("/conf")
+ public String testConfig() {
+ return configBean.toString();
+ }
+
+ @RequestMapping("/conf2")
+ public String testConfig2() {
+ return configBean2.toString();
+ }
+
+ @RequestMapping("/test")
+ public String index() {
+ return testConfigBean.getName() + "," + testConfigBean.getAge();
+ }
+
+ @RequestMapping("/test2")
+ public String index2() {
+ return testConfigBean2.toString();
+ }
}
diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/yml/MixPropertySourceFactory.java b/02.Spring-Boot-Config/src/main/java/com/springboot/yml/MixPropertySourceFactory.java
new file mode 100644
index 00000000..7bd2e7e0
--- /dev/null
+++ b/02.Spring-Boot-Config/src/main/java/com/springboot/yml/MixPropertySourceFactory.java
@@ -0,0 +1,38 @@
+package com.springboot.yml;
+
+import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
+import org.springframework.core.env.PropertiesPropertySource;
+import org.springframework.core.env.PropertySource;
+import org.springframework.core.io.support.DefaultPropertySourceFactory;
+import org.springframework.core.io.support.EncodedResource;
+
+import java.io.IOException;
+import java.util.Properties;
+
+/**
+ * @author :wyq
+ * @date :Created in 2022/1/27
+ * @description :
+ */
+public class MixPropertySourceFactory extends DefaultPropertySourceFactory {
+
+ @Override
+ public PropertySource> createPropertySource(String name, EncodedResource resource) throws IOException {
+ String sourceName = name != null ? name : resource.getResource().getFilename();
+ if (!resource.getResource().exists()) {
+ return new PropertiesPropertySource(sourceName, new Properties());
+ } else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
+ Properties propertiesFromYaml = loadYml(resource);
+ return new PropertiesPropertySource(sourceName, propertiesFromYaml);
+ } else {
+ return super.createPropertySource(name, resource);
+ }
+ }
+
+ private Properties loadYml(EncodedResource resource) throws IOException {
+ YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
+ factory.setResources(resource.getResource());
+ factory.afterPropertiesSet();
+ return factory.getObject();
+ }
+}
diff --git a/02.Spring-Boot-Config/src/main/resources/application-dev.properties b/02.Spring-Boot-Config/src/main/resources/application-dev.properties
index a3ac65ce..53e50368 100644
--- a/02.Spring-Boot-Config/src/main/resources/application-dev.properties
+++ b/02.Spring-Boot-Config/src/main/resources/application-dev.properties
@@ -1 +1 @@
-server.port=8080
\ No newline at end of file
+server.port=8002
\ No newline at end of file
diff --git a/02.Spring-Boot-Config/src/main/resources/application-prod.properties b/02.Spring-Boot-Config/src/main/resources/application-prod.properties
index bafddced..53e50368 100644
--- a/02.Spring-Boot-Config/src/main/resources/application-prod.properties
+++ b/02.Spring-Boot-Config/src/main/resources/application-prod.properties
@@ -1 +1 @@
-server.port=8081
\ No newline at end of file
+server.port=8002
\ No newline at end of file
diff --git a/02.Spring-Boot-Config/src/main/resources/application.properties b/02.Spring-Boot-Config/src/main/resources/application.properties
index 1853bd2b..4bd27d7f 100644
--- a/02.Spring-Boot-Config/src/main/resources/application.properties
+++ b/02.Spring-Boot-Config/src/main/resources/application.properties
@@ -2,4 +2,8 @@ mrbird.blog.name=mrbird's blog
mrbird.blog.title=Spring Boot
mrbird.blog.wholeTitle=${mrbird.blog.name}--${mrbird.blog.title}
+my.config.get.name=abc
+my.config.get.title=test
+my.config.get.wholeTitle=${my.config.get.name}--${my.config.get.title}
+
spring.profiles.active=dev
diff --git a/02.Spring-Boot-Config/src/main/resources/banner.txt b/02.Spring-Boot-Config/src/main/resources/banner.txt
index a3965402..9b065100 100644
--- a/02.Spring-Boot-Config/src/main/resources/banner.txt
+++ b/02.Spring-Boot-Config/src/main/resources/banner.txt
@@ -1,4 +1,4 @@
_ _ _ _ _ _
/ \ / \ / \ / \ / \ / \
-( m | r | b | i | r | d )
+( s | p | r | i | n | g )
\_/ \_/ \_/ \_/ \_/ \_/
diff --git a/02.Spring-Boot-Config/src/main/resources/property.properties b/02.Spring-Boot-Config/src/main/resources/property.properties
new file mode 100644
index 00000000..1e5172dc
--- /dev/null
+++ b/02.Spring-Boot-Config/src/main/resources/property.properties
@@ -0,0 +1,2 @@
+test.name=abc
+test.age=25
\ No newline at end of file
diff --git a/02.Spring-Boot-Config/src/main/resources/property.yml b/02.Spring-Boot-Config/src/main/resources/property.yml
new file mode 100644
index 00000000..dd196d42
--- /dev/null
+++ b/02.Spring-Boot-Config/src/main/resources/property.yml
@@ -0,0 +1,3 @@
+test2:
+ name: property
+ age: 50
\ No newline at end of file
diff --git a/02.Spring-Boot-Config/src/main/resources/test.properties b/02.Spring-Boot-Config/src/main/resources/test.properties
deleted file mode 100644
index 25ef8a30..00000000
--- a/02.Spring-Boot-Config/src/main/resources/test.properties
+++ /dev/null
@@ -1,2 +0,0 @@
-test.name=KangKang
-test.age=25
\ No newline at end of file
diff --git a/03.Spring-Boot-MyBatis/pom.xml b/03.Spring-Boot-MyBatis/pom.xml
index fc26d684..bbb9640c 100644
--- a/03.Spring-Boot-MyBatis/pom.xml
+++ b/03.Spring-Boot-MyBatis/pom.xml
@@ -8,20 +8,20 @@
0.0.1-SNAPSHOT
jar
- demo
+ 03.Spring-Boot-MyBatis
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
- 1.5.9.RELEASE
+ 2.3.4.RELEASE
UTF-8
UTF-8
- 1.7
+ 1.8
@@ -34,7 +34,7 @@
org.mybatis.spring.boot
mybatis-spring-boot-starter
- 1.3.1
+ 2.1.4
@@ -42,16 +42,19 @@
spring-boot-starter-test
test
-
+
+
com.oracle
ojdbc6
- 6.0
+
+ 11.2.0.4.0-atlassian-hosted
+
com.alibaba
druid-spring-boot-starter
- 1.1.6
+ 1.2.4
diff --git a/03.Spring-Boot-MyBatis/readme03.md b/03.Spring-Boot-MyBatis/readme03.md
new file mode 100644
index 00000000..d9fd0c75
--- /dev/null
+++ b/03.Spring-Boot-MyBatis/readme03.md
@@ -0,0 +1,352 @@
+整合MyBatis之前,先搭建一个基本的Spring Boot项目开启Spring Boot。然后引入`mybatis-spring-boot-starter`和数据库连接驱动(这里使用关系型数据库Oracle 11g)。
+
+## mybatis-spring-boot-starter
+
+在pom中引入:
+
+```
+
+ org.mybatis.spring.boot
+ mybatis-spring-boot-starter
+ 2.3.4.RELEASE
+
+```
+
+orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password=gelc123 entries=5 force=y
+
+不同版本的Spring Boot和MyBatis版本对应不一样,具体可查看官方文档:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/。
+
+通过`dependency:tree`命令查看`mybatis-spring-boot-starter`都有哪些隐性依赖:
+
+```
++- org.mybatis.spring.boot:mybatis-spring-boot-starter:jar:1.3.1:compile
+| +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.5.9.RELEASE:compile
+| | +- org.apache.tomcat:tomcat-jdbc:jar:8.5.23:compile
+| | | \- org.apache.tomcat:tomcat-juli:jar:8.5.23:compile
+| | \- org.springframework:spring-jdbc:jar:4.3.13.RELEASE:compile
+| | \- org.springframework:spring-tx:jar:4.3.13.RELEASE:compile
+| +- org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:jar:1.3.1:compile
+| +- org.mybatis:mybatis:jar:3.4.5:compile
+| \- org.mybatis:mybatis-spring:jar:1.3.1:compile
+```
+
+
+
+可发现其包含了`spring-boot-starter-jdbc`依赖,默认使用tomcat-jdbc数据源。
+
+## 引入ojdbc6
+
+由于版权的原因,我们需要将ojdbc6.jar依赖安装到本地的maven仓库,然后才可以在pom中进行配置。
+
+https://www.oracle.com/database/technologies/faq-jdbc.html#02_03
+
+下载ojdbc6.jar文件后,将其放到比较好找的目录下,比如D盘根目录。然后运行以下命令:
+
+```
+C:\Users\Administrator>mvn install:install-file -Dfile=D:/ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=6.0 -
+Dpackaging=jar -DgeneratePom=true
+
+...
+[INFO] --- maven-install-plugin:2.4:install-file (default-cli) @ standalone-pom ---
+[INFO] Installing D:\ojdbc6.jar to D:\m2\repository\com\oracle\ojdbc6\6.0\ojdbc6-6.0.jar
+[INFO] Installing C:\Users\ADMINI~1\AppData\Local\Temp\mvninstall9103688544010617483.pom to D:\m2\repository\com\oracle\ojdbc
+6\6.0\ojdbc6-6.0.pom
+[INFO] ------------------------------------------------------------------------
+[INFO] BUILD SUCCESS
+[INFO] ------------------------------------------------------------------------
+[INFO] Total time: 0.940 s
+[INFO] Finished at: 2017-08-13T15:06:38+08:00
+[INFO] Final Memory: 6M/145M
+[INFO] ------------------------------------------------------------------------
+```
+
+
+
+接着在pom中引入:
+
+```
+
+ com.oracle
+ ojdbc6
+ 6.0
+
+```
+
+这里的groupid就是你之前安装时指定的-Dgroupid的值,artifactid就是你安装时指定的-Dartifactid的值,version也一样。
+
+## Druid数据源
+
+Druid是一个关系型数据库连接池,是阿里巴巴的一个开源项目,地址:https://github.com/alibaba/druid。Druid不但提供连接池的功能,还提供监控功能,可以实时查看数据库连接池和SQL查询的工作情况。
+
+### 配置Druid依赖
+
+Druid为Spring Boot项目提供了对应的starter:
+
+```
+
+ com.alibaba
+ druid-spring-boot-starter
+ 1.1.6
+
+```
+
+
+
+### Druid数据源配置
+
+上面通过查看mybatis starter的隐性依赖发现,Spring Boot的数据源配置的默认类型是`org.apache.tomcat.jdbc.pool.Datasource`,为了使用Druid连接池,需要在application.yml下配置:
+
+```
+#SpringBoot在2.0版本之后已经弃用server.context-path,而代替为server.servlet.context-path
+server:
+ servlet:
+ context-path: /web
+ port: 8003
+
+spring:
+ datasource:
+ druid:
+ # 数据库访问配置, 使用druid数据源
+ type: com.alibaba.druid.pool.DruidDataSource
+ driver-class-name: oracle.jdbc.driver.OracleDriver
+ url: jdbc:oracle:thin:@localhost:1521:ORCL
+ username: scott
+ password: 123456
+ # 连接池配置
+ initial-size: 5
+ min-idle: 5
+ max-active: 20
+ # 连接等待超时时间
+ max-wait: 30000
+ # 配置检测可以关闭的空闲连接间隔时间
+ time-between-eviction-runs-millis: 60000
+ # 配置连接在池中的最小生存时间
+ min-evictable-idle-time-millis: 300000
+ validation-query: select '1' from dual
+ test-while-idle: true
+ test-on-borrow: false
+ test-on-return: false
+ # 打开PSCache,并且指定每个连接上PSCache的大小
+ pool-prepared-statements: true
+ max-open-prepared-statements: 20
+ max-pool-prepared-statement-per-connection-size: 20
+ # 配置监控统计拦截的filters, 去掉后监控界面sql无法统计, 'wall'用于防火墙
+ filters: stat,wall
+ # Spring监控AOP切入点,如x.y.z.service.*,配置多个英文逗号分隔
+ aop-patterns: com.springboot.servie.*
+
+
+ # WebStatFilter配置
+ web-stat-filter:
+ enabled: true
+ # 添加过滤规则
+ url-pattern: /*
+ # 忽略过滤的格式
+ exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
+
+ # StatViewServlet配置
+ stat-view-servlet:
+ enabled: true
+ # 访问路径为/druid时,跳转到StatViewServlet
+ url-pattern: /druid/*
+ # 是否能够重置数据
+ reset-enable: false
+ # 需要账号密码才能访问控制台
+ login-username: druid
+ login-password: druid123
+ # IP白名单
+ # allow: 127.0.0.1
+ # IP黑名单(共同存在时,deny优先于allow)
+ # deny: 192.168.1.218
+
+ # 配置StatFilter
+ filter:
+ stat:
+ log-slow-sql: true
+```
+
+
+
+上述配置不但配置了Druid作为连接池,而且还开启了Druid的监控功能。 其他配置可参考官方wiki——https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
+
+此时,运行项目,访问http://localhost:8080/web/druid:
+
+
+
+输入账号密码即可看到Druid监控后台:
+
+
+
+关于Druid的更多说明,可查看官方wiki——[https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98](https://github.com/alibaba/druid/wiki/常见问题)
+
+## 使用MyBatis
+
+使用的库表:
+
+```
+CREATE TABLE "SCOTT"."STUDENT" (
+ "SNO" VARCHAR2(3 BYTE) NOT NULL ,
+ "SNAME" VARCHAR2(9 BYTE) NOT NULL ,
+ "SSEX" CHAR(2 BYTE) NOT NULL
+);
+
+INSERT INTO "SCOTT"."STUDENT" VALUES ('001', 'KangKang', 'M ');
+INSERT INTO "SCOTT"."STUDENT" VALUES ('002', 'Mike', 'M ');
+INSERT INTO "SCOTT"."STUDENT" VALUES ('003', 'Jane', 'F ');
+```
+
+
+
+创建对应实体:
+
+```
+public class Student implements Serializable{
+ private static final long serialVersionUID = -339516038496531943L;
+ private String sno;
+ private String name;
+ private String sex;
+ // get,set略
+}
+```
+
+
+
+创建一个包含基本CRUD的StudentMapper:
+
+```
+public interface StudentMapper {
+ int add(Student student);
+ int update(Student student);
+ int deleteByIds(String sno);
+ Student queryStudentById(Long id);
+}
+```
+
+
+
+StudentMapper的实现可以基于xml也可以基于注解。
+
+### 使用注解方式
+
+继续编辑StudentMapper:
+
+```
+@Component
+@Mapper
+public interface StudentMapper {
+ @Insert("insert into student(sno,sname,ssex) values(#{sno},#{name},#{sex})")
+ int add(Student student);
+
+ @Update("update student set sname=#{name},ssex=#{sex} where sno=#{sno}")
+ int update(Student student);
+
+ @Delete("delete from student where sno=#{sno}")
+ int deleteBysno(String sno);
+
+ @Select("select * from student where sno=#{sno}")
+ @Results(id = "student",value= {
+ @Result(property = "sno", column = "sno", javaType = String.class),
+ @Result(property = "name", column = "sname", javaType = String.class),
+ @Result(property = "sex", column = "ssex", javaType = String.class)
+ })
+ Student queryStudentBySno(String sno);
+```
+
+
+
+简单的语句只需要使用@Insert、@Update、@Delete、@Select这4个注解即可,动态SQL语句需要使用@InsertProvider、@UpdateProvider、@DeleteProvider、@SelectProvider等注解。具体可参考MyBatis官方文档:http://www.mybatis.org/mybatis-3/zh/java-api.html。
+
+### 使用xml方式
+
+使用xml方式需要在application.yml中进行一些额外的配置:
+
+```
+mybatis:
+ # type-aliases扫描路径
+ # type-aliases-package:
+ # mapper xml实现扫描路径
+ mapper-locations: classpath:mapper/*.xml
+ property:
+ order: BEFORE
+```
+
+
+
+## 测试
+
+接下来编写Service:
+
+```
+public interface StudentService {
+ int add(Student student);
+ int update(Student student);
+ int deleteBysno(String sno);
+ Student queryStudentBySno(String sno);
+}
+```
+
+
+
+实现类:
+
+```
+@Service("studentService")
+public class StudentServiceImp implements StudentService{
+ @Autowired
+ private StudentMapper studentMapper;
+
+ @Override
+ public int add(Student student) {
+ return this.studentMapper.add(student);
+ }
+
+ @Override
+ public int update(Student student) {
+ return this.studentMapper.update(student);
+ }
+
+ @Override
+ public int deleteBysno(String sno) {
+ return this.studentMapper.deleteBysno(sno);
+ }
+
+ @Override
+ public Student queryStudentBySno(String sno) {
+ return this.studentMapper.queryStudentBySno(sno);
+ }
+}
+```
+
+
+
+编写controller:
+
+```
+@RestController
+public class TestController {
+
+ @Autowired
+ private StudentService studentService;
+
+ @RequestMapping( value = "/querystudent", method = RequestMethod.GET)
+ public Student queryStudentBySno(String sno) {
+ return this.studentService.queryStudentBySno(sno);
+ }
+}
+```
+
+完整的项目目录如下图所示:
+
+
+
+启动项目访问:http://localhost:8080/web/querystudent?sno=001:
+
+
+
+查看SQL监控情况:
+
+http://localhost:8003/web/druid/
+
+
+
+可看到其记录的就是刚刚访问/querystudent得到的SQL。
\ No newline at end of file
diff --git a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application.java b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application03.java
similarity index 55%
rename from 03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application.java
rename to 03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application03.java
index a66ea9fe..a99e61aa 100644
--- a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application.java
+++ b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/Application03.java
@@ -5,9 +5,8 @@
@SpringBootApplication
-public class Application {
-
- public static void main(String[] args) {
- SpringApplication.run(Application.class,args);
- }
+public class Application03 {
+ public static void main(String[] args) {
+ SpringApplication.run(Application03.class, args);
+ }
}
diff --git a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/controller/TestController.java b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/controller/TestController.java
index 3fc7b8b0..c9af1db7 100644
--- a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/controller/TestController.java
+++ b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/controller/TestController.java
@@ -13,9 +13,12 @@ public class TestController {
@Autowired
private StudentService studentService;
-
+
+ /**
+ * http://localhost:8003/web/querystudent?sno=002
+ */
@RequestMapping( value = "/querystudent", method = RequestMethod.GET)
public Student queryStudentBySno(String sno) {
- return this.studentService.queryStudentBySno(sno);
+ return studentService.queryStudentBySno(sno);
}
}
diff --git a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/StudentService.java b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/StudentService.java
index fcfd40b5..fc7a11a3 100644
--- a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/StudentService.java
+++ b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/StudentService.java
@@ -4,7 +4,10 @@
public interface StudentService {
int add(Student student);
+
int update(Student student);
+
int deleteBysno(String sno);
+
Student queryStudentBySno(String sno);
}
diff --git a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/impl/StudentServiceImp.java b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/impl/StudentServiceImp.java
index 8983d001..be4a58e1 100644
--- a/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/impl/StudentServiceImp.java
+++ b/03.Spring-Boot-MyBatis/src/main/java/com/springboot/service/impl/StudentServiceImp.java
@@ -2,7 +2,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
-
import com.springboot.bean.Student;
import com.springboot.mapper.StudentMapper;
import com.springboot.service.StudentService;
diff --git a/03.Spring-Boot-MyBatis/src/main/resources/application.yml b/03.Spring-Boot-MyBatis/src/main/resources/application.yml
index 73b2b848..8df7a489 100644
--- a/03.Spring-Boot-MyBatis/src/main/resources/application.yml
+++ b/03.Spring-Boot-MyBatis/src/main/resources/application.yml
@@ -1,5 +1,8 @@
+#SpringBoot在2.0版本之后已经弃用server.context-path,而代替为server.servlet.context-path
server:
- context-path: /web
+ servlet:
+ context-path: /web
+ port: 8003
spring:
datasource:
@@ -8,8 +11,8 @@ spring:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@localhost:1521:ORCL
- username: test
- password: 123456
+ username: scott
+ password: admin12345
# 连接池配置
initial-size: 5
min-idle: 5
diff --git a/11.Spring-Boot-Shiro-Authentication/pom.xml b/11.Spring-Boot-Shiro-Authentication/pom.xml
index 1fdb0b0a..8b4b003b 100644
--- a/11.Spring-Boot-Shiro-Authentication/pom.xml
+++ b/11.Spring-Boot-Shiro-Authentication/pom.xml
@@ -47,7 +47,7 @@
org.apache.shiro
shiro-spring
- 1.4.0
+ 1.7.0
diff --git a/12.Spring-Boot-Shiro-RememberMe/pom.xml b/12.Spring-Boot-Shiro-RememberMe/pom.xml
index c27b864c..97aed20b 100644
--- a/12.Spring-Boot-Shiro-RememberMe/pom.xml
+++ b/12.Spring-Boot-Shiro-RememberMe/pom.xml
@@ -47,7 +47,7 @@
org.apache.shiro
shiro-spring
- 1.4.0
+ 1.7.0
diff --git a/13.Spring-Boot-Shiro-Authorization/pom.xml b/13.Spring-Boot-Shiro-Authorization/pom.xml
index 784930cf..c22e0776 100644
--- a/13.Spring-Boot-Shiro-Authorization/pom.xml
+++ b/13.Spring-Boot-Shiro-Authorization/pom.xml
@@ -47,7 +47,7 @@
org.apache.shiro
shiro-spring
- 1.4.0
+ 1.7.0
diff --git a/14.Spring-Boot-Shiro-Redis/pom.xml b/14.Spring-Boot-Shiro-Redis/pom.xml
index 055ba948..a1ec09bc 100644
--- a/14.Spring-Boot-Shiro-Redis/pom.xml
+++ b/14.Spring-Boot-Shiro-Redis/pom.xml
@@ -47,7 +47,7 @@
org.apache.shiro
shiro-spring
- 1.4.0
+ 1.7.0
diff --git a/15.Spring-Boot-Shiro-Ehcache/pom.xml b/15.Spring-Boot-Shiro-Ehcache/pom.xml
index d579d849..c3e901aa 100644
--- a/15.Spring-Boot-Shiro-Ehcache/pom.xml
+++ b/15.Spring-Boot-Shiro-Ehcache/pom.xml
@@ -47,7 +47,7 @@
org.apache.shiro
shiro-spring
- 1.4.0
+ 1.7.0
diff --git a/16.Spring-Boot-Shiro-Thymeleaf-Tag/pom.xml b/16.Spring-Boot-Shiro-Thymeleaf-Tag/pom.xml
index 250848ff..aededa31 100644
--- a/16.Spring-Boot-Shiro-Thymeleaf-Tag/pom.xml
+++ b/16.Spring-Boot-Shiro-Thymeleaf-Tag/pom.xml
@@ -47,7 +47,7 @@
org.apache.shiro
shiro-spring
- 1.4.0
+ 1.7.0
diff --git a/17.Spring-Boot-Shiro-Session/pom.xml b/17.Spring-Boot-Shiro-Session/pom.xml
index 65f14ebe..bce962a5 100644
--- a/17.Spring-Boot-Shiro-Session/pom.xml
+++ b/17.Spring-Boot-Shiro-Session/pom.xml
@@ -47,7 +47,7 @@
org.apache.shiro
shiro-spring
- 1.4.0
+ 1.7.0