真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

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

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

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

 ==>命名相關(guān)信息

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

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

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

2.1 通過(guò)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 }

 輸出結(jié)果:192.168.3.100-yml,E:selftJavaIdeaPractice

2.2 通過(guò)@value方式讀取配置

==>application.properties配置

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

 ==> 定義類(lèi):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}")//默認(rèn)值:80 9   private int port;
10 
11     @Value("${redis.host:127.0.0.1}")//默認(rèn)值:127.0.0.112   private String host;
13 
14     @Value("${redis.auth:321}")//默認(rèn)值: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 //通過(guò)@Value方式讀取配置5         RedisConfig redisConfig = context.getBean(RedisConfig.class);
6         redisConfig.showConfig();
7         context.close();
8     }

輸出:192.168.3.145:9966,redis-01

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

==>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)建實(shí)體類(lèi):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 //通過(guò)@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 實(shí)際開(kāi)發(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)建專(zhuān)門(mén)的配置類(lèi)文件夾,添加相應(yīng)的配置類(lèi)管理類(lèi),如: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   //屬性構(gòu)造器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 //通過(guò)@ConfigurationProperties方式18         System.out.println("==============通過(guò)@ConfigurationProperties方式start==================");
19         DatasourceProperties dp = context.getBean(DatasourceProperties.class);
20         System.out.println(dp);
21         System.out.println("==============通過(guò)@ConfigurationProperties方式end==================");
22         context.close();
23     }
24 
25 }
main方法

輸出結(jié)果

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

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

資源文件結(jié)構(gòu)

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

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

 設(shè)置指定配置文件

==>運(yùn)行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);

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

        context.close();
    }

}

輸出結(jié)果:192.168.3.150:6680,redis-02

3、指定多個(gè)配置文件

3.1 設(shè)置指定配置文件

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 //通過(guò)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     }

輸出結(jié)果:springBoot-->192.168.3.450:6880,redis-03

4、絕對(duì)路徑指定配置文件 -- 統(tǒng)一配置多項(xià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 //通過(guò)@Value方式讀取配置4         RedisConfig redisConfig = context.getBean(RedisConfig.class);
5         System.out.println("=========================");
6         redisConfig.showConfig();
7 
8         context.close();
9     }

輸出結(jié)果

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

springBoot項(xiàng)目配置總結(jié)

1、配置文件優(yōu)先級(jí)和文件名稱(chēng)

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

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

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

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

3、讀取配置信息

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

(2)通過(guò)@Value

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

(4)@ConfigurationProperties指定配置類(lèi)

4、demo代碼

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

如果該文章對(duì)你有所幫助,請(qǐng)點(diǎn)個(gè)贊支持下,謝謝!
網(wǎng)頁(yè)標(biāo)題:SpringBoot框架(2)--配置文件-創(chuàng)新互聯(lián)
本文來(lái)源:http://weahome.cn/article/ccshdg.html

其他資訊

在線(xiàn)咨詢(xún)

微信咨詢(xún)

電話(huà)咨詢(xún)

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部