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

Mon*_*key 4 java groovy spring spring-4

如何在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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想用配置选项(fe 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图设置"@Configuration"和"@EnableAutoConfiguration"但说实话我不知道他们在做什么.我是Java/Groovy和Spring框架的新手(但不是一般的编程).

我已阅读过这些页面,但没有完整的示例,只有片段:

[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

cod*_*ent 6

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

endpointUrl: http://localhost:5050
Run Code Online (Sandbox Code Playgroud)

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

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)


Vin*_*ati 5

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

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

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

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

@EnableAutoConfiguration: 来自springboot框架。它会自动配置传统的或预期的配置,这些配置以后可能会被覆盖。基本上,它使用不同的 java Configuration 类为不同的默认配置明智地配置默认所需的默认 bean。

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
Run Code Online (Sandbox Code Playgroud)

看起来您遇到了故障排除问题。

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