【发布时间】: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:
[2]https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
【问题讨论】:
-
部署时会设置主机。您可以在 application.yml 中设置上下文名称和端口。你需要多学一点。继续阅读文档。
-
是的,我需要阅读和了解更多信息,但在某些时候你必须开始尝试,这就是我坚持的地方:(
标签: java spring groovy spring-4