gpt4 book ai didi

java - 使用 @PropertySource 和外部 JSON 文件进行 Spring 属性配置

转载 作者:太空宇宙 更新时间:2023-11-04 11:01:34 26 4
gpt4 key购买 nike

我正在使用 spring boot 来访问 keycloak。我需要将 keycloak 配置保留在 keycloak.json 文件中,我想使用 PropertySource 注释从 json 中获取值...

我的应用程序.yml

keycloak:
configurationFile: "classpath:keycloak.json"

我的keycloak.json

{
"realm": "MyRealm",
"auth-server-url": "https://keycloakURL",
....
}

我的类(class)

@Controller
@Configuration
@PropertySource("classpath:keycloak.json")
public class MyClass{

@Autowired
private Environment env;

@RequestMapping(value = "/mycontroller", method = RequestMethod.GET)
public String method(Model model) {
env.getProperty("auth-server-url")
}

问题是我的环境变量为 null

最佳答案

如果 env 为空,则问题可能出在导入上。

org.springframework.core.env.Environment

否则如果从环境中查询的值为空。在 Spring Boot 应用程序中,JSON 文件作为属性的配置有很大不同,请引用 Example 1Example 2

关于java - 使用 @PropertySource 和外部 JSON 文件进行 Spring 属性配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46847688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com