简体   繁体   English

如何在Spring Boot应用程序中使用配置(properties / yml)文件中的属性?

[英]How can I use properties from a configuration (properties/yml) file in my Spring Boot application?

how can I use external configurations within my Spring application? 如何在Spring应用程序中使用外部配置?

package hello.example2.Container

import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.client.RestTemplate

@RestController
class ContainerController {
    @RequestMapping("/container/{cid}")
    public list(@PathVariable Integer cid) {
        def template = new RestTemplate();
        def container = template.getForObject("http://localhost:5050/container/" + cid.toString(), Container);
        return container;
    }
}

I want to replace " http://localhost:5050 " with a configuration option (fe application.yml or application.properties). 我想用配置选项(fe application.yml或application.properties)替换“ http:// localhost:5050 ”。

This is my application file (Groovy): 这是我的应用程序文件(Groovy):

package hello.example2

import groovy.transform.CompileStatic
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.context.annotation.Configuration

@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@CompileStatic
class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

I tried to set "@Configuration" and "@EnableAutoConfiguration" but to be honest I don't know what they are doing. 我试图设置“@Configuration”和“@EnableAutoConfiguration”但说实话我不知道他们在做什么。 I'm new to Java/Groovy and the Spring framework (but not to programming in general). 我是Java / Groovy和Spring框架的新手(但不是一般的编程)。

I have read these pages but there is no complete example only snippets: 我已阅读过这些页面,但没有完整的示例,只有片段:

[1] http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html [1] http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html

[2] https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html [2] https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

In your configuration file (application.yml or application.properties) add a new entry: 在配置文件(application.yml或application.properties)中添加一个新条目:

endpointUrl: http://localhost:5050

Then inject that property in your controller: 然后在您的控制器中注入该属性:

@RestController
class ContainerController {

    @Value("${endpointUrl}")
    private String ednpointUrl;

    @RequestMapping("/container/{cid}")
    public list(@PathVariable Integer cid) {
        def template = new RestTemplate();
        def container = template.getForObject(endpointUrl+"/container/" + cid.toString(), Container);
        return container;
    }
}

Though above answers your question. 虽然上面回答了你的问题。 I'm not that convinced with that. 我不相信这一点。 Let me elaborate one by one: 让我逐一阐述:

@Configuration : You could have spring configuration set in two ways. @Configuration :您可以通过两种方式设置弹簧配置。 a) Using xml file. a)使用xml文件。 b) And using Java Configuration class. b)并使用Java Configuration类。

To use Java class as configuration file for spring, you need to add @Configuration annotation. 要将Java类用作spring的配置文件,需要添加@Configuration批注。 In this Configuration class, now you could specify beans using @Bean tag. 在此Configuration类中,现在可以使用@Bean标记指定bean。 Specify scope as singleton or prototype using @Scope annotation. 使用@Scope注释将范围指定为singleton or prototype And everything you can do using xml configuration file. 您可以使用xml配置文件执行所有操作。

Also, a wide community don't like xmls to be used for configurations and hence, they prefer configuring spring in java classes. 此外,一个广泛的社区不喜欢用于配置的xmls,因此,他们更喜欢在java类中配置spring。

@EnableAutoConfiguration : is from springboot framework. @EnableAutoConfiguration :来自springboot框架。 It automatically configures conventional or expected configurations which could later be overridden. 它会自动配置传统或预期的配置,以后可以覆盖。 Basically it wisely configures default beans required by default using different java Configuration classes for different default configurations. 基本上,它明智地使用不同的Java配置类为不同的默认配置配置默认所需的默认bean。

Regarding externalising configuration : You could externalise configuration to application.properties or application.yml. Regarding externalising configuration :您可以将配置外部化到application.properties或application.yml。 Following should be the location. 以下应该是位置。

  1. A /config subdirectory of the current directory. 当前目录的A / config子目录。
  2. The current directory 当前目录
  3. A classpath /config package 一个classpath / config包
  4. The classpath root 类路径根

The above are ordered in sequence of their precedence. 以上是按优先顺序排列的。 Hence if config directory is present under current project directory. 因此,如果config目录存在于当前项目目录下。 It would have highest precedence. 它将具有最高优先权。

Below I am adding a tree diagram of my application. 下面我要添加我的应用程序的树形图。 May be this helps. 可能这有帮助。

vinayprajapati@localhost:~/Desktop/project/gradleSpringBoot$ tree
.
├── build.gradle
├── gradleSpringBoot.iml
└── src
    ├── main
    │   ├── groovy
    │   │   └── org.test
    │   │       ├── components
    │   │       │   └── TestComponent.groovy
    │   │       ├── configuration
    │   │       │   └── BaseConfiguration.groovy
    │   │       |
    │   │       └── utils
    │   │           ├── AppConfig.groovy
    │   │           └── AppInfo.groovy
    │   ├── java
    │   │   └── org
    │   │       └── test
    │   │           ├── GradleSpringBootApplication.java
    │   │           └── HelloController.java
    │   └── resources
    │       ├── application-qa.yml
    │       ├── application-uat.yml
    │       ├── application.properties
    │       ├── application.yml
    │       ├── static
    │       └── templates
    └── test
        └── java
            └── org
                └── test
                    └── GradleSpringBootApplicationTests.java

Looks like you are having troubleshooting problem. 看起来您遇到了故障排除问题。

Let's do one thing. 我们做一件事。 Put the config file in the resource like in tree diagram above and delete same from all the other places in project. 将配置文件放在上面的树形图中的资源中,并从项目中的所有其他位置删除它。 Reason for doing this is that any config file in the root directory or /config subdirectory has higher precedence and hence would override config which is in classpath. 这样做的原因是根目录或/ config子目录中的任何配置文件具有更高的优先级,因此将覆盖类路径中的配置。

暂无
暂无

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

相关问题 如何使用配置类在 Spring Boot 中为我的应用程序配置属性? - How do I use a configuration class to configure properties for my application in Spring Boot? 当我启动 Spring Boot 应用程序时,它没有从 application.yml 读取属性 - When I start my Spring boot application it is not reading properties from application.yml 如何在 Spring Boot 应用程序的 application.properties 文件中设置 MyBatis 配置属性? - How do I set MyBatis configuration properties in an application.properties file in a Spring Boot application? 如何从 spring-boot 应用程序中的 application.yml 文件中读取属性 - How to read properties from application.yml file in spring-boot application Yml文件中的Spring Boot和属性数据 - Spring Boot and properties data from Yml file 如何在Spring Boot应用程序中从YML文件加载多个属性 - How to load multiple properties from YML file in spring-boot application 来自application.properties的Spring Boot配置 - Spring Boot configuration from application.properties 为什么我无法在 Spring Boot 中使用 application.properties 文件中的属性? - Why I fail to use properties from application.properties file in Spring Boot? Spring Boot - 从 application.yml 和 application.properties 注入 - Spring Boot - inject from application.yml and application.properties 如何从Spring Boot War应用程序访问gradle.properties文件值? - How can I access a gradle.properties file value from a Spring Boot War application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM