【问题标题】:How can I use properties from a configuration (properties/yml) file in my Spring Boot application?如何在我的 Spring Boot 应用程序中使用配置(properties/yml)文件中的属性?
【发布时间】:2017-01-03 02:18:21
【问题描述】:

如何在我的 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;
    }
}

我想用配置选项(例如 application.yml 或 application.properties)替换“http://localhost:5050”。

这是我的应用程序文件(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);
    }
}

我尝试设置“@Configuration”和“@EnableAutoConfiguration”,但老实说我不知道​​他们在做什么。我是 Java/Groovy 和 Spring 框架的新手(但不是一般的编程)。

我已经阅读了这些页面,但没有完整的示例只有 sn-ps:

[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

【问题讨论】:

  • 部署时会设置主机。您可以在 application.yml 中设置上下文名称和端口。你需要多学一点。继续阅读文档。
  • 是的,我需要阅读和了解更多信息,但在某些时候你必须开始尝试,这就是我坚持的地方:(

标签: java spring groovy spring-4


【解决方案1】:

在您的配置文件(application.yml 或 application.properties)中添加一个新条目:

endpointUrl: http://localhost:5050

然后将该属性注入到您的控制器中:

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

【讨论】:

  • 我必须将 application.yml 放在哪里?我把它放在 config/application.yml 下。当我用 gradle (gradle bootRun) 构建项目时,由于[..]/hello/example3/container/ContainerController.groovy: 11: Expected '$endpointUrl' to be an inline constant of type java.lang.String [..],我无法编译
  • application.yml 文件应该在 src/main/resources 下
  • 或者更详细地说,您的 application.yml 或 application.properties 文件应该在类路径中。
  • 我在项目的几乎每个文件夹中都复制了 application.yml 和 application.properties 文件,但仍然收到相同的错误 - 我不知道这个类路径应该在哪里
  • 或者您可以使用 #{} 而不是 ${}。
【解决方案2】:

虽然上面回答了你的问题。我对此并不相信。 让我一一详述:

@Configuration:您可以通过两种方式设置弹簧配置。 a) 使用 xml 文件。 b) 并使用 Java 配置类。

使用Java类作为spring的配置文件,需要添加@Configuration注解。在这个 Configuration 类中,现在您可以使用 @Bean 标记指定 bean。使用@Scope 注释将范围指定为singleton or prototype。以及您可以使用 xml 配置文件执行的所有操作。

此外,广泛的社区不喜欢将 xmls 用于配置,因此他们更喜欢在 java 类中配置 spring。

@EnableAutoConfiguration:来自springboot 框架。它会自动配置以后可能被覆盖的常规或预期配置。基本上,它明智地配置了默认所需的默认bean,使用不同的java配置类来实现不同的默认配置。

Regarding externalising configuration:您可以将配置外部化到 application.properties 或 application.yml。以下应该是位置。

  1. 当前目录的 /config 子目录。
  2. 当前目录
  3. 类路径 /config 包
  4. 类路径根

以上内容按优先顺序排列。因此,如果当前项目目录下存在 config 目录。它将具有最高优先级。

下面我添加了我的应用程序的树形图。可能会有所帮助。

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

您似乎遇到了故障排除问题。

让我们做一件事。将配置文件放在上面的树形图中的资源中,并从项目中的所有其他位置删除相同的文件。这样做的原因是根目录或 /config 子目录中的任何配置文件都具有更高的优先级,因此会覆盖类路径中的配置。

【讨论】:

  • 感谢您的精彩概述。我现在把我的配置放在 src/main/resources 下。我想我会在开发的后期回到@Configuration@EnableAutoConfiguration。再次感谢你! (我也不喜欢 XML)
猜你喜欢
  • 2021-05-23
  • 2018-06-20
  • 2016-07-17
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-15
  • 2016-04-09
相关资源
最近更新 更多