如何使用Spring Boot从java属性文件读取数据

时间:2023-02-05 13:10:04

I have a spring boot application and I want to read some variable from my application.properties file. In fact below codes do that. But I think there is a good method for this alternative.

我有一个spring boot应用程序,我想从应用程序中读取一些变量。属性文件。事实上,下面的代码可以做到这一点。但是我认为有一个很好的方法来替代它。

Properties prop = new Properties();
InputStream input = null;

try {
    input = new FileInputStream("config.properties");
    prop.load(input);
    gMapReportUrl = prop.getProperty("gMapReportUrl");
} catch (IOException ex) {
    ex.printStackTrace();
} finally {
    ...
}

2 个解决方案

#1


23  

You can use @PropertySource to externalize your configuration to a properties file. There is number of way to do get properties:

可以使用@PropertySource将配置外部化到属性文件。获取属性的方法有很多:

1. Assign the property values to fields by using @Value with PropertySourcesPlaceholderConfigurer to resolve ${} in @Value:

1。通过使用@Value和PropertySourcesPlaceholderConfigurer将属性值分配给字段,以解析@Value中的${}:

@Configuration
@PropertySource("file:config.properties")
public class ApplicationConfiguration {

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

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

2. Get the property values by using Environment:

2。使用环境获取属性值:

@Configuration
@PropertySource("file:config.properties")
public class ApplicationConfiguration {

    @Autowired
    private Environment env;

    public void foo() {
        env.getProperty("gMapReportUrl");
    }

}

Hope this can help

希望这可以帮助

#2


7  

i would suggest the following way:

我的建议是:

@PropertySource(ignoreResourceNotFound = true, value = "classpath:otherprops.properties")
@Controller
public class ClassA {
    @Value("${myName}")
    String name;

    @RequestMapping(value = "/xyz")
    @ResponseBody
    public void getName(){
        System.out.println(name);
    }
}

Here your new properties file name is "otherprops.properties" and the property name is "myName". This is the simplest implementation to access properties file in spring boot version 1.5.8.

这里您的新属性文件名是“其他道具”。属性和属性名是“myName”。这是在spring boot version 1.5.8中访问属性文件的最简单实现。

#1


23  

You can use @PropertySource to externalize your configuration to a properties file. There is number of way to do get properties:

可以使用@PropertySource将配置外部化到属性文件。获取属性的方法有很多:

1. Assign the property values to fields by using @Value with PropertySourcesPlaceholderConfigurer to resolve ${} in @Value:

1。通过使用@Value和PropertySourcesPlaceholderConfigurer将属性值分配给字段,以解析@Value中的${}:

@Configuration
@PropertySource("file:config.properties")
public class ApplicationConfiguration {

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

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

2. Get the property values by using Environment:

2。使用环境获取属性值:

@Configuration
@PropertySource("file:config.properties")
public class ApplicationConfiguration {

    @Autowired
    private Environment env;

    public void foo() {
        env.getProperty("gMapReportUrl");
    }

}

Hope this can help

希望这可以帮助

#2


7  

i would suggest the following way:

我的建议是:

@PropertySource(ignoreResourceNotFound = true, value = "classpath:otherprops.properties")
@Controller
public class ClassA {
    @Value("${myName}")
    String name;

    @RequestMapping(value = "/xyz")
    @ResponseBody
    public void getName(){
        System.out.println(name);
    }
}

Here your new properties file name is "otherprops.properties" and the property name is "myName". This is the simplest implementation to access properties file in spring boot version 1.5.8.

这里您的新属性文件名是“其他道具”。属性和属性名是“myName”。这是在spring boot version 1.5.8中访问属性文件的最简单实现。