简体   繁体   English

使用@PropertySource和外部JSON文件配置Spring属性

[英]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 使用正确的方法@ValuePropertySource中描述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.

相关问题 使用@PropertySource的Spring属性配置 - Spring properties configuration using @PropertySource 使用Spring PropertySource无法通过绝对路径找到属性文件 - Cannot find a properties file by absolute path using Spring PropertySource 无法使用@PropertySource将.properties文件注入Spring MVC 4.3 - Not able to inject .properties file into Spring MVC 4.3 using @PropertySource 如何在Spring中使用@PropertySource加载的属性文件中配置“动态键” - how to configure 'dynamic key' in properties file loaded using @PropertySource in Spring 如何模拟在@Configuration 中注入@PropertySource 的.properties 文件? - How to mock a .properties file injected with @PropertySource in @Configuration? SPRING MVC - 使用外部文件作为@PropertySource - SPRING MVC - Use an external file as @PropertySource 使用相应的专用“属性”文件(使用@PropertySource)设置时会覆盖单个弹簧“beans”属性值 - Individual spring "beans" properties value overridden while setting with corresponding dedicated "properties" file (using @PropertySource) Spring PropertySource找不到的属性 - Properties not found with Spring PropertySource 我正在使用 @PropertySource 和 @ConfigurationProperties 的组合,但我想用外部属性文件覆盖它们 - I am using a combination of @PropertySource and @ConfigurationProperties but I want to overwrite them with an external properties file 如何使用SpringBoot PropertySource从外部配置文件读取 - How to use SpringBoot PropertySource to read from an external configuration file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM