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

MonkeyMonkey:

如何在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框架的新手(但通常不熟悉编程)。

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

[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或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;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Spring Boot应用程序的application.properties文件中设置MyBatis配置属性?

如何在Spring-boot应用程序中使用配置文件?

如何在我的application.properties文件的Spring Boot应用程序中配置HikariCP?

在Spring Boot应用程序中配置Spring Security时,如何在没有@Configuration的类中使用@Bean?

如何在Spring Boot应用程序中配置PageableHandlerMethodArgumentResolver

在Spring-Boot应用程序中使用application.yaml配置hystrix命令属性

如何在Spring Boot中用yml文件替换属性文件

在Spring Boot应用程序的应用程序属性文件中使用Jenkins变量

如何使用配置类在 Spring Boot 中为我的应用程序配置属性?

我可以在Spring Boot应用程序中使用xml属性文件而不是application.properties吗?

如何配置Spring Boot应用程序以使用两个消息属性文件?

如何在应用程序属性中设置文件位置-Spring Boot

如何从azure应用程序设置或azure功能应用程序的文件中使用Serilog配置?

如何在 Spring Boot 应用程序的自定义 yaml 文件中使用环境变量?

在Spring Boot应用程序中使用Flyway时,如何在H2中加载初始数据?

如何在 Spring Boot 中使用 logback 从所有应用程序日志中删除新行

如何使用Spring Security在Spring Boot应用程序中配置RSocket安全

无论在何处定义,如何在Spring Boot应用程序中获取有效的Properties?

如何在ReactJs应用程序中读取Spring Boot application.properties?

如何在VSCode中运行Spring Boot Maven项目以及如何配置Spring Boot Web应用程序的基本URL

如何在Spring Boot应用程序中记录活动配置?

如何在Spring Boot应用程序中配置Embedded MongDB以进行集成测试?

如何在Spring Boot应用程序中配置HikariCP和Dropwizard / Coda-Hale指标

如何在Spring Boot应用程序中为ehCache配置Dropwizard / CodaHale指标?

如何在Spring Boot Web应用程序中配置2个单独的过滤器?

使用PropertiesLauncher启动的Spring-boot(特定于配置文件)应用程序中的问题覆盖应用程序属性

如何使用Maven在现有的Spring Boot应用程序中利用属性文件?

如何在Spring Boot应用程序中使用JPQL配置JPA-“无法解析符号..”

如何在Spring Boot应用程序中使用命令行参数读取自定义属性文件