SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
1 SpringBoot內(nèi)部配置
1.1、修改端口和應(yīng)用上下文
server.port=9090 server.context-path=/hellobootSpring Boot也可以使用yml進(jìn)行配置,application.yml配置方式:
server:port:9090contextPath:/helloboot1.2、使用XML配置
SpringBoot提倡零配置,即無(wú)xml配置,但是在實(shí)際項(xiàng)目中,可能有一些特殊要求你必須使用XML配置,這時(shí)我們可以通過(guò)Spring提供的@ImportResource來(lái)加載xml配置。例如:
@ImportResource({"classpath:some-context.xml","classpath:another-context.xml"})2、SpringBoot外部配置
Spring Boot允許使用properties文件,yaml文件或者命令行參數(shù)作為外部配置
2.1 命令行參數(shù)配置
Spring Boot可以是基于jar包運(yùn)行的,打成jar包的程序可以直接通過(guò)下面的命令運(yùn)行:
java -jar xx.jar可以通過(guò)以下命令修改Tomcat端口號(hào):
java -jar xx.jar --server.port=90902.2 常規(guī)屬性配置
在2.2節(jié)我們講述了在常規(guī)Spring環(huán)境下,注入properties文件里的值的方式,通過(guò)@PropertySource指明properties文件的位置,然后通過(guò)@Value注入值。在Spring Boot里,我們只需要在application.properties定義屬性,直接使用@Value注入即可。例如:
@Configuration @PropertySource({ "classpath:config.properties", "classpath:db.properties" //如果是相同的key,則最后一個(gè)起作用 }) public class AppConfig { @Autowired Environment env; }2.3完整案例
2.3.1 目錄結(jié)構(gòu)
2.3.2 編寫pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.toto</groupId><artifactId>ch5_2_4</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>demo</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.7.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>2.3.3 application.properties文件的內(nèi)容
server.port=9090 server.context-path=/demobook.author=wangyunfei book.name=spring boot2.3.4 banner.txt參考博文
http://blog.csdn.net/tototuzuoquan/article/details/78168952
2.3.5 DemoApplication.java內(nèi)容如下
package com.toto.ch5_2_4;import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;/*** @RestController 等價(jià)@ResponseBody 和 @Controller*/ @RestController @SpringBootApplication public class DemoApplication {@Value("${book.author}")private String bookAuthor;@Value("${book.name}")private String bookName;@RequestMapping("/")String index() {return "book name is:" + bookName + " and book author is:" + bookAuthor;}public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);} }2.3.6 DemoApplicationTests.java的內(nèi)容如下:
package com.toto.ch5_2_4;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests {@Testpublic void contextLoads() {}}2.3.7 瀏覽器訪問(wèn)
http://localhost:9090/demo/
2.4 類型安全配置
Spring Boot還提供了基于類型安全的配置方式,通過(guò)@ConfigurationProperties將properties屬性和一個(gè)Bean及其屬性關(guān)聯(lián),從而實(shí)現(xiàn)類型安全的配置
2.4.1 項(xiàng)目目錄結(jié)構(gòu)
2.4.2 pom.xml文件的內(nèi)容和上面的案例的內(nèi)容一樣,這里省略不做介紹
2.4.3 author.properties 的內(nèi)容如下
author.name=toto author.age=272.4.4 AuthorSettings的內(nèi)容如下
package com.toto.ch5_2_4;import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component;@Component /**注意在1.5以上的SpringBoot版本中已經(jīng)將location去掉了,這里使用PropertySource作為替代方案**/ @ConfigurationProperties(prefix = "author") @PropertySource("classpath:config/author.properties") public class AuthorSettings {private String name;private String age;public String getName() {return name;}public String getAge() {return age;}public void setName(String name) {this.name = name;}public void setAge(String age) {this.age = age;} }2.4.5 DemoApplication.java的內(nèi)容如下:
package com.toto.ch5_2_4;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;/*** @RestController 等價(jià)@ResponseBody 和 @Controller*/ @RestController @SpringBootApplication public class DemoApplication {@Autowiredprivate AuthorSettings authorSettings;@RequestMapping("/")String index() {return "author name is:" + authorSettings.getName() + " and author age is:" + authorSettings.getAge();}public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);} }2.4.6 運(yùn)行查看結(jié)果
3、日志配置
Spring Boot支持Java Util Logging、Log4J、Log4J2和logback作為日志框架,無(wú)論使用哪種日志框架,Spring Boot已為當(dāng)前使用日志框架的控制臺(tái)輸出及文件輸出做好了配置。
默認(rèn)情況下,Spring Boot使用Logback作為日志框架。
配置日志級(jí)別和最終的輸出路徑:
logging.file=E:/workspace/log.log配置日志文件,格式為logging.level.包名=級(jí)別 logging.level.org.springframework.web = DEBUG4、Profile配置
profile是Spring用來(lái)針對(duì)把不同的環(huán)境對(duì)不同的配置提供支持的,全局Profile使用application-{profile}.properties如(application-prod.properties)
也就是說(shuō):Spring可以針對(duì)不同的環(huán)境使用不同的配置文件做配置
通過(guò)在application.properties中設(shè)置spring.profiles.active=prod來(lái)指定使用配置文件application-prod.properties中的配置
下面我們分別為生產(chǎn)(prod)和開發(fā)(dev)環(huán)境使用不同的配置文件,生產(chǎn)環(huán)境下端口號(hào)為80,開發(fā)環(huán)境下端口為8888
4.1 項(xiàng)目目錄結(jié)構(gòu)
4.2 編寫配置文件
生產(chǎn)環(huán)境的配置文件application-prod.properties的內(nèi)容如下:
server.port=80 server.context-path=/prod開發(fā)環(huán)境的配置文件application-dev.properties的內(nèi)容如下:
server.port=8080 server.context-path=/dev當(dāng)前的application.properties的配置文件內(nèi)容為:
## 可以到E:/workspace/log.log中查看日志內(nèi)容 logging.file=E:/workspace/log.log logging.level.org.springframework.web = DEBUGspring.profiles.active=dev4.3 運(yùn)行
瀏覽器中輸入:http://localhost:8080/dev/,看到的結(jié)果如下:
修改application.properties中的spring.profiles.active的值為prod,即:
logging.file=E:/workspace/log.log logging.level.org.springframework.web = DEBUGspring.profiles.active=prod然后再運(yùn)行,在瀏覽器上輸入:
http://localhost/prod/,運(yùn)行后的結(jié)果如下:
經(jīng)過(guò)查看兩次結(jié)果,發(fā)現(xiàn)最后兩次的運(yùn)行結(jié)果一致。
總結(jié)
以上是生活随笔為你收集整理的SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 打完苜蓿净多长时间能种燕麦,打完苜蓿茎多
- 下一篇: 广州这边租电动车不租了退回去会不会收折旧