springboot获取默认配置文件中的配置项

springboot默认的配置文件是application.properties。如何获取里面的配置项的值呢。

默认配置路径是在classpath根目录【resources】下,或者在classpath:/config目录下

application.properties配置文件中有一下两个配置:

local.ip=192.168.1.100
local.port=8088

方式一:在启动类里获取

@SpringBootApplication
public class MylslApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MylslApplication.class, args);
        System.err.println("local.ip=" + context.getEnvironment().getProperty("local.ip"));
    }

}

输出:

方式二:在类中利用Environment获取

    新建一个类如下:

    

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class MyConfig {

    @Autowired
    private Environment env;

    public void show(){
        System.err.println("MyConfig---local.ip=" + env.getProperty("local.ip"));
    }
}

  在启动类里调用下该show()方法,看下打印结果

package com.lsl.mylsl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;


@SpringBootApplication
public class MylslApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MylslApplication.class, args);
        System.err.println("local.ip=" + context.getEnvironment().getProperty("local.ip"));
        context.getBean(MyConfig.class).show();
    }

}

输出结果:

方式三:在类中利用@Value注解获取,直接赋值给属性

package com.lsl.mylsl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class MyConfig {

    @Autowired
    private Environment env;

    @Value("${local.port}")
    private String localPort;

    public void show(){
        System.err.println("MyConfig---local.ip=" + env.getProperty("local.ip"));
        System.err.println("MyConfig---local.port=" + env.getProperty("local.port"));
    }
}

结果输出:

另外,配置文件支持引用配置

local.ip=192.168.1.100
local.port=8088

name=springboot
app.name=this name ${name}
package com.lsl.mylsl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class MyConfig {

    @Autowired
    private Environment env;

    @Value("${local.port}")
    private String localPort;

    public void show(){
        System.err.println("MyConfig---local.ip=" + env.getProperty("local.ip"));
        System.err.println("MyConfig---local.port=" + env.getProperty("local.port"));
        System.err.println("MyConfig---name=" + env.getProperty("name"));
        System.err.println("MyConfig---app.name=" + env.getProperty("app.name"));
    }
}

输出结果

默认配置文件及路径的重新指定

配置文件的名字和路径也是可以修改的,通过命令来重新指定

修改文件名字:--spring.config.name=app.properties

修改路径:--spring.config.location=classpath:/conf/app.properties;file:e:/temp/app.properties

修改路径可以是系统文件的,利用file:/来修改。

另外也可以通过编程的方式指定配置文件

比如在classpath:/conf下有个配置文件app.properties

myusername=rootroot
password=123456

 

写一个配置类,利用@PropertySource注解引入配置文件

如果需要指定多个配置文件,可以在PropertiesConfig加多个@PropertySource注解

也可以利用@PropertySources注解一次指定多个配置文件

@PropertySources({@PropertySource("classpath:/conf/app.properties"),@PropertySource("file:/e:/temp/jdbc.properties")})

方式四:利用注解@ConfigurationProperties(prefix = "ds")获取带前缀的配置项

myusername=rootroot
password=123456

ds.url=http://192.168.1.200:8080/index.html
ds.name=LSL
package com.lsl.mylsl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "ds")
public class MyConfig {

    @Autowired
    private Environment env;

    @Value("${local.port}")
    private String localPort;

    @Value("${myusername}")
    private String myusername;

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

    private String url;

    private String name;

    public void show(){
        System.err.println("MyConfig---local.ip=" + env.getProperty("local.ip"));
        System.err.println("MyConfig---local.port=" + env.getProperty("local.port"));
        System.err.println("MyConfig---name=" + env.getProperty("name"));
        System.err.println("MyConfig---app.name=" + env.getProperty("app.name"));
        System.err.println("MyConfig---myusername=" + env.getProperty("myusername"));
        System.err.println("MyConfig---password=" + env.getProperty("password"));
        System.err.println("MyConfig---name=" + name + ",url="+url);
    }

    public String getUrl() {
        return url;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

输出结果

对于带前缀的配置项,当然也可以通过上面的方式获取,也可以采用第四种方式获取,属性名必须后配置项后缀名字一致,且注解@ConfigurationProperties(prefix = "ds")配置好前缀,且配置类里有setter\getter方法,这样才可以。

最后说下,如果获取配置文件中配置项集合

yusername=rootroot
password=123456

ds.url=http://192.168.1.200:8080/index.html
ds.name=LSL

ds.ports[0]=9000
ds.ports[1]=9001
ds.ports[2]=9002
package com.lsl.mylsl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@ConfigurationProperties(prefix = "ds")
public class MyConfig {

    @Autowired
    private Environment env;

    @Value("${local.port}")
    private String localPort;

    @Value("${myusername}")
    private String myusername;

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

    private String url;

    private String name;
    //注入list
    private List<String> ports = new ArrayList<>();

    public void show(){
        System.err.println("MyConfig---local.ip=" + env.getProperty("local.ip"));
        System.err.println("MyConfig---local.port=" + env.getProperty("local.port"));
        System.err.println("MyConfig---name=" + env.getProperty("name"));
        System.err.println("MyConfig---app.name=" + env.getProperty("app.name"));
        System.err.println("MyConfig---myusername=" + env.getProperty("myusername"));
        System.err.println("MyConfig---password=" + env.getProperty("password"));
        System.err.println("MyConfig---name=" + name + ",url="+url);
        System.err.println("MyConfig---ports=" + ports );

    }

    public List<String> getPorts() {
        return ports;
    }

    public void setPorts(List<String> ports) {
        this.ports = ports;
    }

    public String getUrl() {
        return url;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

输出结果

猜你喜欢

转载自blog.csdn.net/dhklsl/article/details/114985492