[英]Spring properties configuration using @PropertySource with external JSON file
I am using spring boot with an access to keycloak. 我正在使用带有引导钥匙斗篷的弹簧靴。 I need to leave the keycloak configuration in the keycloak.json file I would like to get teh values from the json using the PropertySource annotation... 我需要将keycloak配置保留在keycloak.json文件中,我想使用PropertySource批注从json获取值。
My application.yml 我的application.yml
keycloak:
configurationFile: "classpath:keycloak.json"
My keycloak.json 我的keycloak.json
{
"realm": "MyRealm",
"auth-server-url": "https://keycloakURL",
....
}
My 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")
}
The problem is that i am getting null for this environment variable 问题是我为此环境变量获取null
If env is null, maybe the issue is with the import. 如果env为null,则可能是导入问题。
org.springframework.core.env.Environment org.springframework.core.env.Environment
Else if the value queried from the environment is empty. 否则,如果从环境查询的值为空。 In spring boot application, configuration of JSON file as property is quite different please refer the Example 1 , Example 2 在Spring Boot应用程序中,将JSON文件配置为属性有很大不同,请参阅示例1 , 示例2
I know using @Value
is somewhat discouraged in favor of env.getProperty()
, but I found the former much more reliable and straight forward. 我知道不建议使用@Value
来支持env.getProperty()
,但是我发现前者更加可靠和直接。 The proper way to use @Value
from a PropertySource
is described in mkyong's great tutorial 使用正确的方法@Value
从PropertySource
中描述mkyong的伟大的教程
To resolve ${} in @Values, you must register a static PropertySourcesPlaceholderConfigurer in either XML or annotation configuration file. 要在@Values中解析$ {},必须在XML或注释配置文件中注册静态PropertySourcesPlaceholderConfigurer。
目前尚不可能,但是我们有一张票https://issues.jboss.org/browse/KEYCLOAK-4942
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.