简体   繁体   English

通过@Value 获取属性并以编程方式将其放入 application.yml 中。 Java 与 SpringBoot

[英]Get properties by @Value and put it into application.yml programmatically. Java with SpringBoot

I use Azure KeyVault to store some values.我使用Azure KeyVault来存储一些值。 I need to get this values (eg mysql-uri or client-secret ) and create new properties in application.yml (and application-local.yml).我需要获取这些值(例如mysql-uriclient-secret )并在 application.yml(和 application-local.yml)中创建新属性。 First of all, I tried to create Configuration-class with @Bean like getDataSource to create connection to database and I did it successfully, but I need to add other fields as well, such as 'oauth.client.secret'.首先,我尝试使用像 getDataSource 这样的@Bean创建 Configuration-class 来创建与数据库的连接并且我成功地做到了,但是我还需要添加其他字段,例如“oauth.client.secret”。

So I tried to get values and create 'properties' in main-class, but @Value couldn't be static and this solution throws NPE .所以我试图在主类中获取值并创建“属性”,但@Value不能是静态的,这个解决方案会抛出NPE I tried to create new Configuration-class and get properties from it and then pull it into SpringApplicationBuilder , but I need ApplicationContext (so SpringApplication.run will be called) to get an instance (bean) of this class with values...我尝试创建新的 Configuration-class 并从中获取属性,然后将其拉入SpringApplicationBuilder ,但我需要 ApplicationContext (因此SpringApplication.run将被调用)来获取此类的实例(bean)和值...

I don't know what to do next, I'm stuck.我不知道接下来要做什么,我被困住了。 Ready to rewrite and show any solution you need.准备重写并展示您需要的任何解决方案。

UPDATE 1:更新1:

    @Value("${secret}")
    private String clientSecret;

    public static void main(String[] args) {
        new SpringApplicationBuilder(DefaultApplication.class).properties(getProperties()).run(args);        
    }

    @NotNull
    private static Properties getProperties() {
        Properties properties = new Properties();
        properties.put("oauth.client.secret", Objects.requireNonNull(clientSecret));

        return properties;
    }

clientSecret with error: Non-static field 'clientSecret' cannot be referenced from a static context . clientSecret 出现错误:无法从静态上下文中引用非静态字段“clientSecret”

You can try this way of injecting properties.你可以试试这种注入属性的方式。 This uses a simpler convenience class SpringApplication instead of SpringApplicationBuilder .这使用了一个更简单的便利类SpringApplication而不是SpringApplicationBuilder

MainClass.java主类.java

@SpringBootApplication
public class MainClass {
    public static void main(String... args) {
        SpringApplication.run(MainClass.class, args);
    }
}

The application.yaml will be automatically loaded into the context by Spring Boot (unless you have configured against it). application.yaml 将被 Spring Boot 自动加载到上下文中(除非你已经针对它进行了配置)。 Properties can be reused, redefined (using profiles) and system/env variables can be used as well.属性可以重用、重新定义(使用配置文件),也可以使用系统/环境变量。

NOTE Spring configurations are pretty flexible and powerful.注意Spring 配置非常灵活和强大。 Please read this documentation https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config请阅读此文档https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config

application.yaml应用程序.yaml

app:
  name: Demo
oauth:
  client:
    secret: ${app.name} // This will have "Demo"
env:
  path: ${SOME_ENV_VAR} // This will take SOME_ENV_VAR from env variable

SomeService.java SomeService.java

@Service
public class SomeService {
    @Value("${app.name})
    private String appName;

    public void printAppName() {
        log.info(appName); // will log the appname
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java 8 Springboot 如何在注释中使用 application.yml 文件中的属性? - java 8 Springboot How to use properties from application.yml file in annotations? 如何在springboot中从application.yml中读取带有特殊字符的属性 - How to read properties with special characters from application.yml in springboot 使用application.yml配置SpringBoot - SpringBoot Configuration with application.yml 无法访问 springboot 组件中的 application.yml 属性 - Not able to access application.yml properties in springboot component application.yml 和 @value - application.yml and @value Java Spring 引导自定义属性在 application.yml 中不起作用 - Java Spring Boot custom properties dont work in application.yml 无法从 static 方法中的 application.yml 获取值 - Cannot get value from application.yml in static method 将私钥放入带有变量的 application.yml - Put private key in application.yml with variable springboot kafka value.serializer = StringSerializer 尽管 producerConfig 和 application.yml 显示否则 - springboot kafka value.serializer = StringSerializer despite producerConfig and the application.yml showing otherwise 获取应用程序名称Springboot application.yml时获得NullPointerException - Got NullPointerException when getting application name Springboot application.yml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM