springboot gets the configuration items in the default configuration file

The default configuration file of springboot is application.properties. How to get the value of the configuration item inside.

The default configuration path is under the classpath root directory [resources], or under the classpath:/config directory

 

There are two configurations in the application.properties configuration file:

local.ip=192.168.1.100
local.port=8088

Method 1: Get it in the startup class

@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"));
    }

}

Output:

 

 

Method 2: Use Environment to obtain in the class

    Create a new class as follows:

    

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"));
    }
}

  Call the show() method in the startup class and see the print results.

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();
    }

}

Output result:

 

 

Method three: Use the @Value annotation to obtain it in the class and assign it directly to the attribute.

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"));
    }
}

Result output:

 

In addition, the configuration file supports reference configuration

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"));
    }
}

 

Output results

 

Re-specification of default configuration files and paths

The name and path of the configuration file can also be modified and respecified through commands

Modify the file name: --spring.config.name=app.properties

Modify the path: --spring.config.location=classpath:/conf/app.properties;file:e:/temp/app.properties

The modified path can be a system file, use file:/ to modify it.

Alternatively, you can specify the configuration file programmatically

For example, there is a configuration file app.properties under classpath:/conf

myusername=rootroot
password=123456

 

 

Write a configuration class and use the @PropertySource annotation to introduce the configuration file

 

 

If you need to specify multiple configuration files, you can add multiple @PropertySource annotations to PropertiesConfig.

You can also use the @PropertySource s annotation to specify multiple configuration files at once

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

 

Method 4: Use the annotation @ConfigurationProperties(prefix = "ds") to obtain the prefixed configuration items

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;
    }
}

 

Output results

 

For configuration items with prefixes, you can of course obtain them through the above method, or you can also obtain them through the fourth method. The attribute name must be the same as the suffix name of the configuration item, and the prefix must be configured with the annotation @ConfigurationProperties(prefix = "ds"). And there are setter\getter methods in the configuration class, so this is possible.

 

Finally, if you get the collection of configuration items in the configuration file

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;
    }
}

Output results

Guess you like

Origin blog.csdn.net/dhklsl/article/details/114985492