成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

SpringBoot框架(2)--配置文件-創(chuàng)新互聯(lián)

1、添加新項目,選擇Spring Initializr方式創(chuàng)建項目

成都創(chuàng)新互聯(lián)公司專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、三亞網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5建站商城建設、集團公司官網(wǎng)建設、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為三亞等各大城市提供網(wǎng)站開發(fā)制作服務。

 ==>命名相關信息

2、默認配置讀取順序 -- /config/路徑下優(yōu)先,xxx.properties 比 xxx.yml 優(yōu)先

   /resourses/config/application.properties > /resourses/config/application.yml > /resourses/application.properties > /resourses/config/application.yml

注意:默認讀取的配置文件必須命名為:application,否者讀取不到。

2.1 通過Environment方式讀取

==> /resources/application.properties文件

1 local.ip.addr=192.168.3.110-pro
 1 package com.demo.boot.bootconfig;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.context.ConfigurableApplicationContext;
 6 import org.springframework.core.env.Environment;
 7 
 8 @SpringBootApplication
 9 public class BootConfigApplication {
10 
11   public static void main(String[] args) {
12         ConfigurableApplicationContext context = SpringApplication.run(BootConfigApplication.class, args);
13         Environment env = context.getEnvironment();
14         String ip = env.getProperty("local.ip.addr");
15         String currentDir = env.getProperty("user.dir");
16         System.out.println(ip + "," + currentDir);
17         context.close();
18     }
19 
20 }

 輸出結果:192.168.3.100-yml,E:selftJavaIdeaPractice

2.2 通過@value方式讀取配置

==>application.properties配置

1 redis.port=9966
2 redis.host:192.168.3.145
3 redis.auth:redis-01

 ==> 定義類:RedisConfig

 1 package com.demo.boot.bootconfig;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.stereotype.Component;
 5 
 6 @Component
 7 public class RedisConfig {
 8     @Value("${redis.port:80}")//默認值:80 9   private int port;
10 
11     @Value("${redis.host:127.0.0.1}")//默認值:127.0.0.112   private String host;
13 
14     @Value("${redis.auth:321}")//默認值:32115   private String auth;
16 
17   public int getPort() {
18 return port;
19     }
20 
21   public void setPort(int port) {
22 this.port = port;
23     }
24 
25   public String getHost() {
26 return host;
27     }
28 
29   public void setHost(String host) {
30 this.host = host;
31     }
32 
33   public String getAuth() {
34 return auth;
35     }
36 
37   public void setAuth(String auth) {
38 this.auth = auth;
39     }
40 
41   public void showConfig() {
42         System.out.print(host + ":" + port + "," + auth + "
");
43     }
44 
45 }
1 public static void main(String[] args) {
2         ConfigurableApplicationContext context = SpringApplication.run(BootConfigApplication.class, args);
3 
4 //通過@Value方式讀取配置5         RedisConfig redisConfig = context.getBean(RedisConfig.class);
6         redisConfig.showConfig();
7         context.close();
8     }

輸出:192.168.3.145:9966,redis-01

2.3 通過@ConfigurationProperties方式讀取配置(配置類屬性名與配置文件屬性名一致)

==>application.properties文件添加以下內(nèi)容

1 boot.datasource.driverClassName = com.mysql.jdbc.Driver
2 boot.datasource.url = jdbc:mysql:///db3 boot.datasource.username = root
4 boot.datasource.password = 456

==>創(chuàng)建實體類:DatasourceProperties.java

 1 package com.demo.boot.bootconfig;
 2 
 3 import org.springframework.boot.context.properties.ConfigurationProperties;
 4 import org.springframework.stereotype.Component;
 5 
 6 @Component
 7 @ConfigurationProperties(prefix = "boot.datasource")
 8 public class DatasourceProperties {
 9   private String driverClassName;
10   private String url;
11   private String userName;
12   private String password;
13 
14   public String getDriverClassName() {
15 return driverClassName;
16     }
17 
18   public void setDriverClassName(String driverClassName) {
19 this.driverClassName = driverClassName;
20     }
21 
22   public String getUrl() {
23 return url;
24     }
25 
26   public void setUrl(String url) {
27 this.url = url;
28     }
29 
30   public String getUserName() {
31 return userName;
32     }
33 
34   public void setUserName(String userName) {
35 this.userName = userName;
36     }
37 
38   public String getPassword() {
39 return password;
40     }
41 
42   public void setPassword(String password) {
43 this.password = password;
44     }
45 
46     @Override
47   public String toString() {
48 return "DatasourceProperties{" +
49                 "driverClassName='" + driverClassName + ''' +
50                 ", url='" + url + ''' +
51                 ", userName='" + userName + ''' +
52                 ", password='" + password + ''' +
53                 '}';
54     }
55 }

==>main方法調(diào)用

1 public static void main(String[] args) {
2         ConfigurableApplicationContext context = SpringApplication.run(BootConfigApplication.class, args);
3 
4 //通過@ConfigurationProperties方式5         DatasourceProperties dbConfig = context.getBean(DatasourceProperties.class);
6         System.out.println(dbConfig);
7 
8         context.close();
9     }

輸出:DatasourceProperties{driverClassName='com.mysql.jdbc.Driver', url='jdbc:mysql:///db', userName='root', password='456'}

2.4 實際開發(fā)中為了方便調(diào)試,把2.3方式稍微更改一下(推薦)

==>application.properties添加配置信息

1 boot.datasource.driverClassName = com.mysql.jdbc.Driver
2 boot.datasource.url = jdbc:mysql:///db3 boot.datasource.username = dev-boot
4 boot.datasource.password = 456

==>創(chuàng)建專門的配置類文件夾,添加相應的配置類管理類,如:config/DatasourceConfig.java

==>config/DatasourceConfig.java

 1 package com.demo.boot.bootconfig.config;
 2 
 3 import com.demo.boot.bootconfig.DatasourceProperties;
 4 import org.springframework.boot.SpringBootConfiguration;
 5 import org.springframework.boot.context.properties.ConfigurationProperties;
 6 import org.springframework.context.annotation.Bean;
 7 
 8 @SpringBootConfiguration
 9 public class DatasourceConfig {
10     @Bean
11     @ConfigurationProperties(prefix = "boot.datasource")
12   public DatasourceProperties dp() {
13 return new DatasourceProperties();
14     }
15 }

==>改造DatasourceProperties.java,去掉注解

//@Component
//@ConfigurationProperties(prefix = "boot.datasource")
 1 package com.demo.boot.bootconfig;
 2 
 3 import org.springframework.boot.context.properties.ConfigurationProperties;
 4 import org.springframework.stereotype.Component;
 5 
 6 //@Component
 7 //@ConfigurationProperties(prefix = "boot.datasource") 8 public class DatasourceProperties {
 9   private String driverClassName;
10   private String url;
11   private String userName;
12   private String password;
13   //屬性構造器14 }
DatasourceProperties .java

==>main方法調(diào)用

 1 package com.demo.boot.bootconfig;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.boot.context.properties.ConfigurationProperties;
 7 import org.springframework.context.ConfigurableApplicationContext;
 8 import org.springframework.context.annotation.Bean;
 9 import org.springframework.core.env.Environment;
10 
11 @SpringBootApplication
12 public class BootConfigApplication {
13   public static void main(String[] args) {
14         ConfigurableApplicationContext context = SpringApplication.run(BootConfigApplication.class, args);
15 
16 
17 //通過@ConfigurationProperties方式18         System.out.println("==============通過@ConfigurationProperties方式start==================");
19         DatasourceProperties dp = context.getBean(DatasourceProperties.class);
20         System.out.println(dp);
21         System.out.println("==============通過@ConfigurationProperties方式end==================");
22         context.close();
23     }
24 
25 }
main方法

輸出結果

==============通過@ConfigurationProperties方式start==================
DatasourceProperties{driverClassName='com.mysql.jdbc.Driver', url='jdbc:mysql:///db', userName='dev-boot', password='456'}
==============通過@ConfigurationProperties方式end==================

3、指定配置文件(.properties > .yml)

資源文件結構

/config/app.yml文件添加一下內(nèi)容

1 redis:
2   auth: redis-02
3   host: 192.168.3.150
4   port: 6680

 設置指定配置文件

==>運行main方法

package com.demo.boot.bootconfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

@SpringBootApplication
public class BootConfigApplication {
public static void main(String[] args) {
        ConfigurableApplicationContext context= SpringApplication.run(BootConfigApplication.class, args);

//通過@Value方式讀取配置        RedisConfig redisConfig = context.getBean(RedisConfig.class);
        redisConfig.showConfig();

        context.close();
    }

}

輸出結果:192.168.3.150:6680,redis-02

3、指定多個配置文件

3.1 設置指定配置文件

3.2 config/app.yml文件添加以下內(nèi)容

1 redis:
2   auth: redis-03
3   host: 192.168.3.450
4   port: 6880

 config/app1.properties文件添加以下內(nèi)容

1 redis.appName=springBoot

3.3 main方法中打印配置信息

 1public static void main(String[] args) {
 2         ConfigurableApplicationContext context = SpringApplication.run(BootConfigApplication.class, args);
 3 //通過Evironment方式讀取配置 4         Environment env = context.getEnvironment(); 
 5 
 6         String port = env.getProperty("redis.port");
 7         String host = env.getProperty("redis.host");
 8         String auth = env.getProperty("redis.auth");
 9         String appName = env.getProperty("redis.appName");
10 
11         System.out.println(appName + "-->" + host + ":" + port + "," + auth);
12         context.close();
13     }

輸出結果:springBoot-->192.168.3.450:6880,redis-03

4、絕對路徑指定配置文件 -- 統(tǒng)一配置多項目配置

==>創(chuàng)建配置文件 E:/Java/app.properties

1 boot.datasource.driverClassName = com.mysql.jdbc.Driver
2 boot.datasource.url = jdbc:mysql:///db3 boot.datasource.username = file-config
4 boot.datasource.password = abc132

==>新建FileConfig.java

 1 package com.demo.boot.bootconfig;
 2 
 3 import org.springframework.context.annotation.Configuration;
 4 import org.springframework.context.annotation.PropertySource;
 5 
 6 @Configuration
 7 //@PropertySource("classpath:config/app.yml")//不支持.yml
 8 //@PropertySource("classpath:config/app1.properties") 9 @PropertySource("file:/E:/Java/app.properties")
10 public class FileConfig {
11 }

==>main方法調(diào)用

1 public static void main(String[] args) {
2         ConfigurableApplicationContext context = SpringApplication.run(BootConfigApplication.class, args);
3 //通過@Value方式讀取配置4         RedisConfig redisConfig = context.getBean(RedisConfig.class);
5         System.out.println("=========================");
6         redisConfig.showConfig();
7 
8         context.close();
9     }

輸出結果

=========================
127.0.0.1:80,321
DatasourceProperties{driverClassName='com.mysql.jdbc.Driver', url='jdbc:mysql:///db', userName='file-config', password='abc132'}

springBoot項目配置總結

1、配置文件優(yōu)先級和文件名稱

==> config/application.properties > config/application.yml > application.properties > application.yml > (指定路徑)file:/E:/tem/app.properties

2、指定配置文件(名稱/路徑)

-- spring.config.name=xxxx/xxx/app(不帶后綴)

-- spring.config.location=classpath:xxx.(properties|yml)[,classpath=xxx.(properties|yml),file:D:/tem/xxx.properties](帶文件后綴名,多個配置使用“,”隔開)

3、讀取配置信息

(1)通過環(huán)境變量Evironment

(2)通過@Value

(3)通過@Configuration + @PropertySource("指定文件路徑") -- 不支持.yml文件

(4)@ConfigurationProperties指定配置類

4、demo代碼

 https://github.com/LF20160912/boot-proj/tree/master/boot-config

如果該文章對你有所幫助,請點個贊支持下,謝謝!

文章名稱:SpringBoot框架(2)--配置文件-創(chuàng)新互聯(lián)
本文鏈接:http://jinyejixie.com/article16/ccshdg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、建站公司、外貿(mào)建站、商城網(wǎng)站、網(wǎng)站維護、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
海晏县| 白城市| 崇礼县| 新巴尔虎左旗| 新绛县| 大邑县| 六盘水市| 澎湖县| 贺兰县| 容城县| 泗水县| 太仆寺旗| 大石桥市| 罗城| 岐山县| 西华县| 安图县| 安图县| 黑水县| 噶尔县| 如东县| 马龙县| 苏州市| 阳东县| 新竹县| 宜都市| 耿马| 海伦市| 田阳县| 水富县| 应用必备| 上饶市| 潜江市| 咸丰县| 平顶山市| SHOW| 民和| 遵义市| 商城县| 桑植县| 修武县|