From 0e9622e795f32a0329f5f7b4f699cdd6cb8faddd Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 22:23:45 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=BC=80=E5=90=AFspringboot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/{DemoApplication.java => Application01.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename 01.Spring-Boot-Start/src/main/java/com/springboot/demo/{DemoApplication.java => Application01.java} (83%) diff --git a/01.Spring-Boot-Start/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.Spring-Boot-Start/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.Spring-Boot-Start/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); } } From 0c6918ef82452acc1329f44114dfca8c164072bf Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 22:24:20 +0800 Subject: [PATCH 2/6] =?UTF-8?q?Spring=20Boot=E5=9F=BA=E7=A1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.Spring-Boot-Config/readme02.md | 224 ++++++++++++++++++ .../{Application.java => Application02.java} | 6 +- 2 files changed, 228 insertions(+), 2 deletions(-) create mode 100644 02.Spring-Boot-Config/readme02.md rename 02.Spring-Boot-Config/src/main/java/com/springboot/{Application.java => Application02.java} (76%) 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/),页面显示如下: + +![QQ截图20171130112735.png](https://mrbird.cc/img/QQ%E6%88%AA%E5%9B%BE20171130112735.png) + +在属性非常多的情况下,也可以定义一个和配置文件对应的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 76% 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 9f1a53ed..8a339e5b 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,5 +1,6 @@ package com.springboot; +import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; @@ -10,10 +11,11 @@ @SpringBootApplication @EnableConfigurationProperties({ConfigBean.class,TestConfigBean.class}) //@ImportResource({"classpath:some-application.xml"}) -public class Application { +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); } } From c4bb71cb139ec9e24d35990dff0d8e687a88e5f7 Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 22:36:56 +0800 Subject: [PATCH 3/6] =?UTF-8?q?Spring=20Boot=E5=9F=BA=E7=A1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02.Spring-Boot-Config/pom.xml | 8 +++++++- .../main/java/com/springboot/Application02.java | 2 +- .../java/com/springboot/bean/BlogProperties.java | 9 ++++++++- .../main/java/com/springboot/bean/ConfigBean.java | 12 ++++++++++-- .../springboot/controller/IndexController.java | 15 +++++++++++++-- 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/02.Spring-Boot-Config/pom.xml b/02.Spring-Boot-Config/pom.xml index 56ba3fb7..3335efa2 100644 --- a/02.Spring-Boot-Config/pom.xml +++ b/02.Spring-Boot-Config/pom.xml @@ -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 diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java index 8a339e5b..bdbd1857 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java @@ -15,7 +15,7 @@ public class Application02 { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application02.class); app.setAddCommandLineProperties(false); - app.setBannerMode(Banner.Mode.OFF); +// app.setBannerMode(Banner.Mode.OFF); app.run(args); } } 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/BlogProperties.java index 15694fc6..b0e45d7d 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/BlogProperties.java @@ -27,5 +27,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/bean/ConfigBean.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean.java index 2a57ee13..c95e74d4 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 @@ -24,6 +24,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/controller/IndexController.java b/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java index cd4348bb..dc1bf985 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 @@ -10,6 +10,7 @@ @RestController +@RequestMapping("/v2") public class IndexController { @Autowired private BlogProperties blogProperties; @@ -18,8 +19,18 @@ public class IndexController { @Autowired private TestConfigBean testConfigBean; - @RequestMapping("/") - String index() { + @RequestMapping("/blog") + public String testBlog(){ + return blogProperties.toString(); + } + + @RequestMapping("/conf") + public String testConfig(){ + return configBean.toString(); + } + + @RequestMapping("/test") + public String index() { return testConfigBean.getName() + "," + testConfigBean.getAge(); } } From 29c31bf0a63b43c16591880d1eb5b3b7dbf9c627 Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 23:05:46 +0800 Subject: [PATCH 4/6] =?UTF-8?q?Spring=20Boot=E5=9F=BA=E7=A1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/springboot/Application02.java | 4 +- .../java/com/springboot/bean/ConfigBean.java | 7 +++ .../java/com/springboot/bean/ConfigBean2.java | 45 +++++++++++++++++++ .../com/springboot/bean/TestConfigBean.java | 42 ++++++++++------- ...roperties.java => ValueGetProperties.java} | 7 ++- .../controller/IndexController.java | 19 ++++++-- .../main/resources/application-dev.properties | 2 +- .../resources/application-prod.properties | 2 +- .../src/main/resources/application.properties | 4 ++ 9 files changed, 106 insertions(+), 26 deletions(-) create mode 100644 02.Spring-Boot-Config/src/main/java/com/springboot/bean/ConfigBean2.java rename 02.Spring-Boot-Config/src/main/java/com/springboot/bean/{BlogProperties.java => ValueGetProperties.java} (79%) diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java index bdbd1857..4189f4d4 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java @@ -9,8 +9,8 @@ import com.springboot.bean.TestConfigBean; @SpringBootApplication -@EnableConfigurationProperties({ConfigBean.class,TestConfigBean.class}) -//@ImportResource({"classpath:some-application.xml"}) +@EnableConfigurationProperties({ConfigBean.class}) +//@EnableConfigurationProperties({ConfigBean.class,TestConfigBean.class}) public class Application02 { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application02.class); 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 c95e74d4..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; 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/TestConfigBean.java b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/TestConfigBean.java index 46673053..66e1c409 100644 --- 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 @@ -5,24 +5,32 @@ import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; -@Configuration -@ConfigurationProperties(prefix="test") +/** + * @author: wyq + * @create time: 2022/1/27 + * @description: @PropertySource读取指定 properties文件 + */ +@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; - } - + 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 79% 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 b0e45d7d..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; 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 dc1bf985..d1ce64a9 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,10 +1,11 @@ package com.springboot.controller; +import com.springboot.bean.ConfigBean2; 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.ValueGetProperties; import com.springboot.bean.ConfigBean; import com.springboot.bean.TestConfigBean; @@ -13,22 +14,32 @@ @RequestMapping("/v2") public class IndexController { @Autowired - private BlogProperties blogProperties; + private ValueGetProperties blogProperties; + @Autowired private ConfigBean configBean; + + @Autowired + private ConfigBean2 configBean2; + @Autowired private TestConfigBean testConfigBean; @RequestMapping("/blog") - public String testBlog(){ + public String testBlog() { return blogProperties.toString(); } @RequestMapping("/conf") - public String testConfig(){ + public String testConfig() { return configBean.toString(); } + @RequestMapping("/conf2") + public String testConfig2() { + return configBean2.toString(); + } + @RequestMapping("/test") public String index() { return testConfigBean.getName() + "," + testConfigBean.getAge(); 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 From 23027194e1ef6c2b55255e8307091abac9fc4fce Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 23:07:56 +0800 Subject: [PATCH 5/6] =?UTF-8?q?Spring=20Boot=E5=9F=BA=E7=A1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/springboot/Application02.java | 5 ++--- .../bean/{TestConfigBean.java => PropertySourceGet.java} | 2 +- .../main/java/com/springboot/controller/IndexController.java | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) rename 02.Spring-Boot-Config/src/main/java/com/springboot/bean/{TestConfigBean.java => PropertySourceGet.java} (95%) diff --git a/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java index 4189f4d4..68637c6c 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/Application02.java @@ -1,16 +1,15 @@ package com.springboot; -import org.springframework.boot.Banner; +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}) -//@EnableConfigurationProperties({ConfigBean.class,TestConfigBean.class}) +//@EnableConfigurationProperties({ConfigBean.class, PropertySourceGet.class}) public class Application02 { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application02.class); 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/PropertySourceGet.java similarity index 95% rename from 02.Spring-Boot-Config/src/main/java/com/springboot/bean/TestConfigBean.java rename to 02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet.java index 66e1c409..08b3acdf 100644 --- a/02.Spring-Boot-Config/src/main/java/com/springboot/bean/TestConfigBean.java +++ b/02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet.java @@ -13,7 +13,7 @@ @ConfigurationProperties(prefix = "test") @PropertySource("classpath:test.properties") @Component -public class TestConfigBean { +public class PropertySourceGet { private String name; private int age; 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 d1ce64a9..c1eec6e3 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 @@ -7,7 +7,7 @@ import com.springboot.bean.ValueGetProperties; import com.springboot.bean.ConfigBean; -import com.springboot.bean.TestConfigBean; +import com.springboot.bean.PropertySourceGet; @RestController @@ -23,7 +23,7 @@ public class IndexController { private ConfigBean2 configBean2; @Autowired - private TestConfigBean testConfigBean; + private PropertySourceGet testConfigBean; @RequestMapping("/blog") public String testBlog() { From 71d2231b6c0e4f6bbab49322123e989e275f38f4 Mon Sep 17 00:00:00 2001 From: xiaowangzi123 <1103694415@qq.com> Date: Thu, 27 Jan 2022 23:26:21 +0800 Subject: [PATCH 6/6] =?UTF-8?q?Spring=20Boot=E5=9F=BA=E7=A1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springboot/bean/PropertySourceGet.java | 4 +- .../springboot/bean/PropertySourceGet2.java | 44 +++++++++++++++++++ .../controller/IndexController.java | 13 +++--- .../yml/MixPropertySourceFactory.java | 38 ++++++++++++++++ .../{test.properties => property.properties} | 0 .../src/main/resources/property.yml | 3 ++ 6 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 02.Spring-Boot-Config/src/main/java/com/springboot/bean/PropertySourceGet2.java create mode 100644 02.Spring-Boot-Config/src/main/java/com/springboot/yml/MixPropertySourceFactory.java rename 02.Spring-Boot-Config/src/main/resources/{test.properties => property.properties} (100%) create mode 100644 02.Spring-Boot-Config/src/main/resources/property.yml 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 index 08b3acdf..9b0460a0 100644 --- 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 @@ -9,9 +9,11 @@ * @author: wyq * @create time: 2022/1/27 * @description: @PropertySource读取指定 properties文件 + * @PropertySource不支持加载yml配置文件,所以需要自定义一个配置类 */ @ConfigurationProperties(prefix = "test") -@PropertySource("classpath:test.properties") +@PropertySource("classpath:property.properties") +//@PropertySource("classpath:property.yml") //此时获取name和age分别为null,0 @Component public class PropertySourceGet { private String name; 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/controller/IndexController.java b/02.Spring-Boot-Config/src/main/java/com/springboot/controller/IndexController.java index c1eec6e3..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,14 +1,10 @@ package com.springboot.controller; -import com.springboot.bean.ConfigBean2; +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.ValueGetProperties; -import com.springboot.bean.ConfigBean; -import com.springboot.bean.PropertySourceGet; - @RestController @RequestMapping("/v2") @@ -24,6 +20,8 @@ public class IndexController { @Autowired private PropertySourceGet testConfigBean; + @Autowired + private PropertySourceGet2 testConfigBean2; @RequestMapping("/blog") public String testBlog() { @@ -44,4 +42,9 @@ public String testConfig2() { 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/test.properties b/02.Spring-Boot-Config/src/main/resources/property.properties similarity index 100% rename from 02.Spring-Boot-Config/src/main/resources/test.properties rename to 02.Spring-Boot-Config/src/main/resources/property.properties 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