获取配置文件中的属性

spring boot的工程启动的时候,内部文件默认是加载classpath路径或者classpath:/config目录下的application.properties文件的。当然也可以指定加载其它的配置文件,如何获取配置文件中的属性呢?实例如下:

【1】、获取默认classpath下的application.properties配置文件中的属性

application.properties中的内容为:

local.ip=127.0.0.1
  • 1

一、获取classpath下的application.properties文件中属性

1、直接通过配置文件的环境变量获取 
启动类:

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        ConfigurableApplicationContext app = SpringApplication.run(App.class, args);
    /*通过getEnvironment函数默认获取到的文件即为application.properties,然后用getProperty函数从文件中获取键的属性*/
    System.out.println(app.getEnvironment().getProperty("local.ip"));
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

或者创建一个类,用来获取配置属性,例如

package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/*注意:由于@SpringBootApplication中有一个注解@ComponentScan自动扫描启动类所在的包,而PropertiesConfig正好在启动包下,如果
 * 该类不与启动类在同一包下,需要该类加载到spring boot的启动源中*/
@Component
public class PropertiesConfig {

    @Autowired
    private Environment env;

    public void show(){     
    System.out.println(env.getProperty("local.ip"));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

启动类中的方法为:

        ConfigurableApplicationContext app = SpringApplication.run(App.class, args);
        app.getBean(PropertiesConfig.class).show();
  • 1
  • 2

当运行启动类的时候,输出如下:

127.0.0.1
  • 1

2、用@Value注解来获取 
新建一个配置类,专门用来获取application.properties文件中的属性。

package com.example.demo;
import 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

/*注意:由于@SpringBootApplication中有一个注解@ComponentScan自动扫描启动类所在的包,而PropertiesConfig正好在启动包下,如果
 * 该类不与启动类在同一包下,需要该类加载到spring boot的启动源中*/
@Component
public class PropertiesConfig {

    /*通过@Value注解会默认把application.properties中的local.ip键对应的属性自动赋给localIp属性*/
    @Value("${local.ip}")
    private String localIp;

    public void show(){
        System.out.println("localIp=" + localIp);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

启动类为:

        ConfigurableApplicationContext app = SpringApplication.run(App.class, args);
        app.getBean(PropertiesConfig.class).show();
  • 1
  • 2

当运行启动类的时候,输出如下:

127.0.0.1
  • 1

【2】、获取自定义的properties配置文件属性

当在工程目录下自定义了配置文件,例如在/src/main/resources/下的/fileConfig的目录下创建一个jdbc.properties文件,或者在服务器中非工程目录下D:\tmp创建一个jdbc1.properties配置文件。 如果启动spring boot工程的时候,系统默认是没有加载该配置文件的,因此在系统环境中获取不到该配置文件,若想获取到该配置文件中的属性,可以通过下面两种方式进行获取:在启动的参数中指定启动自定义配置文件;在配置类上指定要加载的自定义配置文件。

自定义jdbc.properties配置文件:

jdbc.username=lzj
jdbc.password=123456
  • 1
  • 2

自定义jdbc1.properties配置文件:

jdbc.username1=lzj
jdbc.password1=123456
jdbc.url=jdbc:mysql://localhost:3306/databasename
  • 1
  • 2
  • 3

一、在启动spring boot的参数中进行指定 
run as -> run configurations -> arguments 
在program arguments框中填入下列参数:

--spring.config.location=classpath:/fileConfig/jdbc.properties, file:D:/tmp/jdbc1.properties
  • 1

如果配置文件直接在classpath或者classpath下的config目录下,例如app.properties,也可以直接如下配置:

--spring.config.name=app
  • 1

如按上面配置后,spring boot启动的时候会自动加载指定的文件,下面获取自定义文件中的属性就和获取默认classpath下的application.properties配置文件中的属性的方法完全一致,请参考上面获取方式。

二、在配置类上指定要加载的自定义配置文件 
1、例如还是要获取classpath下的/fileConfig/jdbc.properties和D:/tmp/jdbc1.properties的自定义配置文件属性,现在不需要在启动的参数中进行指定了,定义一个配置类,在配置类中进行指定加载的自定义配置文件。 
配置类ConfigTest :

package com.example.demo;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@Configuration
@PropertySource("classpath:/fileConfig/jdbc.properties")
@PropertySource("file:D:/tmp/jdbc1.properties")
/*或者按如下配置:
@PropertySources({@PropertySource("classpath:/fileConfig/jdbc.properties"), @PropertySource("file:D:/tmp/jdbc.properties")})*/
public class ConfigTest {

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

配置完要加载的配置文件后,下面就是要获取自定义的配置文件中的属性了。 
创建获取classpath:/fileConfig/jdbc.properties的bean类JdbcTest:

package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class JdbcTest {

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    public void show(){
        System.out.println("username=" + username);
        System.out.println("password=" + password);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

创建获取D:/tmp/jdbc1.properties的bean的类JdbcFile :

package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class JdbcFile {

    @Value("${jdbc.username1}")
    private String username;

    @Value("${jdbc.password1}")
    private String password;

    @Value("${jdbc.url}")
    private String url;

    public void show(){
        System.out.println("username=" + username);
        System.out.println("password=" + password);
        System.out.println("url=" + url);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

启动类如下:

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        ConfigurableApplicationContext app = SpringApplication.run(App.class, args);
    app.getBean(JdbcTest.class).show();
    app.getBean(JdbcFile.class).show();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

输出结果为:

username=lzj
password=123456
username=zhangsan
password=654321
url=jdbc:mysql://localhost:3306/databasename
  • 1
  • 2
  • 3
  • 4
  • 5

2、上面一种方式通过配置类加载自定义配置文件后,然后通过@value注解从配置类中获取键值。下面一种方式也是先通过配置类加载自定义的配置文件,但是通过@ConfigurationProperties注解自动把配置文件中的键对应的值注入到相应的属性中。例如: 
配置类ConfigTest 同上:

package com.example.demo;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@Configuration
@PropertySource("classpath:/fileConfig/jdbc.properties")
@PropertySource("file:D:/tmp/jdbc1.properties")
/*或者按如下配置:
@PropertySources({@PropertySource("classpath:/fileConfig/jdbc.properties"), @PropertySource("file:D:/tmp/jdbc.properties")})*/
public class ConfigTest {

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

下面创建一个类SourceProperties,用来获取自定义配置文件中的键值,类中的属性一定要和自定义配置文件中的键的名字保持一致,否则不能把配置文件中键对应的值注入到类对象的属性中。在上面的配置类中加载了classpath:/fileConfig/jdbc.properties和file:D:/tmp/jdbc1.properties两个自定义的配置文件,在SourceProperties类中只需要定义两个配置文件中包含的键为类对象的属性,然后通过set方法把配置文件中键对应的值注入到类对象的属性中就可以了。

package com.example.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
/*prefix可以指定配置文件中键的前缀。例如配置文件中有一个键为jdbc.username,配置了prefix="jdbc"后,在下面的属性中只需要定义username就可以了。在值注入的时候,就会自动把配置文件中的jdbc.username对应的值自动注入到username属性中*/
@ConfigurationProperties(prefix="jdbc")
public class SourceProperties {

    /*username对应jdbc.properties文件中的jdbc.username键*/
    private String username;
    /*username1对应jdbc1.properties文件中的jdbc.username1键*/
    private String username1;
    /*password对应jdbc.properties文件中的jdbc.password键*/
    private String password;
    /*password1对应jdbc1.properties文件中的jdbc.password1键*/
    private String password1;
    /*url对应jdbc1.properties文件中的jdbc.url键*/
    private String url;

    /*一定要给需要注入的属性设置get方法,否则值注入不进来*/
    public void setUsername(String username) {
        this.username = username;
    }

    public void setUsername1(String username1) {
        this.username1 = username1;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPassword1(String password1) {
        this.password1 = password1;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void show(){
        System.out.println("---username=" + username);
        System.out.println("---username1=" + username1);
        System.out.println("---password=" + password);
        System.out.println("---password=" + password1);
        System.out.println("---url=" + url);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

启动类为:

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        ConfigurableApplicationContext app = SpringApplication.run(App.class, args);
    app.getBean(SourceProperties.class).show();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

运行启动类,输出结果为:

---username=lzj
---username1=zhangsan
---password=123456
---password=654321
---url=jdbc:mysql://localhost:3306/databasename
  • 1
  • 2
  • 3
  • 4
  • 5

可见完全取到了自定义配置文件中的键值。

【3】、另外,配置文件中还可以定义变量,可以为配置文件中没有定义的键赋一个默认的值

1、在配置文件中定义变量 
下面以spring boot默认加载的application.properties为例,在该配置文件中配置如下,先定义了一个name的键值对,然后又定义了一个local.name的键值对,但local.name中的键值对引用了name的键值对。

name=lzj
local.name=hello ${name}
  • 1
  • 2

启动类为:

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        ConfigurableApplicationContext app = SpringApplication.run(App.class, args);
    System.out.println(app.getEnvironment().getProperty("name"));
        System.out.println(app.getEnvironment().getProperty("local.name"));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

运行启动类,输出结果为:

lzj
hello lzj
  • 1
  • 2

可见,获得了配置文件中键对应的值。

2、为配置文件中没有定义的键默认赋一个值。 
还是以spring boot默认加载的application.properties为例,其配置内容为:

local.ip=127.0.0.1
  • 1

配置文件中只配置了local.ip,没有配置local.port,下面我们从配置文件中获取local.port,如果不存在的话,就默认赋一个值。创建一个PropertiesConfig 类,用来获取键值。

package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/*注意:由于@SpringBootApplication中有一个注解@ComponentScan自动扫描启动类所在的包,而PropertiesConfig正好在启动包下,如果
 * 该类不与启动类在同一包下,需要该类加载到spring boot的启动源中*/
@Component
public class PropertiesConfig {

    /*从配置文件取local.port对应的值,如果不存在,就默认赋一个8888的值。*/
    @Value("${local.port:8888}")
    private Integer localPort;

    public void show(){
    System.out.println("local.port=" + localPort);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

启动类为:

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        ConfigurableApplicationContext app = SpringApplication.run(App.class, args);
        app.getBean(PropertiesConfig.class).show();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

运行启动类,输出:

local.port=8888
  • 1

版权声明:转载请注明出处 https://blog.csdn.net/u010502101/article/details/78702093

猜你喜欢

转载自blog.csdn.net/gangsijay888/article/details/81254904