SpringBoot框架(2)--配置文件-创新互联

1、添加新项目,选择Spring Initializr方式创建项目

成都创新互联公司专注于企业成都全网营销、网站重做改版、三亚网站定制设计、自适应品牌网站建设、H5建站商城建设、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为三亚等各大城市提供网站开发制作服务。

 ==>命名相关信息

2、默认配置读取顺序 -- /config/路径下优先,xxx.properties 比 xxx.yml 优先

   /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文件添加以下内容

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

==>创建实体类: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方法调用

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 实际开发中为了方便调试,把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

==>创建专门的配置类文件夹,添加相应的配置类管理类,如: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方法调用

 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文件添加一下内容

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文件添加以下内容

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

 config/app1.properties文件添加以下内容

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、绝对路径指定配置文件 -- 统一配置多项目配置

==>创建配置文件 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方法调用

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、配置文件优先级和文件名称

==> 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)通过环境变量Evironment

(2)通过@Value

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

(4)@ConfigurationProperties指定配置类

4、demo代码

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

如果该文章对你有所帮助,请点个赞支持下,谢谢!
本文标题:SpringBoot框架(2)--配置文件-创新互联
文章源于:http://azwzsj.com/article/ccshdg.html