简体   繁体   English

春季启动参数化

[英]Spring-boot parameterizing

I am new to Spring-bot and I need to add a configuration file inside the resources folder and read values from it. 我是Spring-bot的新手,我需要在resources文件夹中添加一个配置文件并从中读取值。 Right now what I have done is that I have created a 'Configuration.properties' file in the classpath folder and have invoked the properties inside the program 现在,我要做的是在类路径文件夹中创建了一个“ Configuration.properties”文件,并在程序中调用了属性

FileReader reader=new FileReader("Configuration.properties");
Properties p=new Properties(); 
p.load(reader);
return p;

Can somebody please help how can i make it to call from the application.properties file of the spring-boot(or similar files) and thus make my configurations available inside the jar which I create. 有人可以帮我如何从spring-boot的application.properties文件(或类似文件)中调用它,从而使我的配置在我创建的jar中可用。

PS: The Configurations i have given is project specific and am using them for avoiding the hardcoding inside the code. PS:我给出的配置是特定于项目的,正在使用它们以避免代码内部的硬编码。

You can use the @Value annotation to get the property file values. 您可以使用@Value批注获取属性文件的值。

Ex : 例如:

@Value("${rest_api_url}")
private String restApiUrl;

Create application.properties file in the resource folder, spring boot will automatically find it. 在资源文件夹中创建application.properties文件,Spring Boot会自动找到它。

Say you use these properties 假设您使用这些属性

test.stringProperty=will_be_used
test.integerProperty=42
stringProperty=not_used

You can then create a configuration properties class like so 然后,您可以像这样创建配置属性类

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "test")
public static class TestProperties {

    private String stringProperty;
    private Integer integerProperty;

    // getters and setters

}

and use it 并使用它

import org.springframework.context.annotation.Configuration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@Configuration
@EnableConfigurationProperties(TestProperties.class)
public class TestConfiguration {

    @Autowired
    private TestProperties config;

    // do whatever with properties

}

The autowired object will then have the values you specified in application.properties file with the name 'prefix'.'name of the variable'. 然后,自动装配的对象将具有您在application.properties文件中指定的值,名称为'prefix'。'变量名'。

Or you can use the 或者您可以使用

@Value("${stringProperty}")
private String value;

which is easier at first, but not very maintainable for higher number of properties. 起初比较容易,但是对于更多的属性却不太可维护。

Need to define Property file as per requirement of your app. 需要根据您的应用要求定义属性文件。 You can use the @Value annotation in bean. 您可以在bean中使用@Value批注。
refer : Externalized Configuration and Properties and Configuration 请参阅: 外部化配置以及属性和配置

Create a file in resource folder by the name application.properties which will be taken automatically as the default property file. 在资源文件夹中创建名为application.properties的文件,该文件将自动作为默认属性文件。

If you want to specify some other file as a property, do the below-mentioned changes. 如果要将其他文件指定为属性,请进行以下更改。

Snippet : 片段:

@SpringBootApplication
@ImportResource("classpath:filename.properties")
public class Example{
    public static void main(String[] args) {
    SpringApplication.run(Example.class, args);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM