简体   繁体   English

是否可以在自定义 Spring boot starter 中使用默认 application.yml?

[英]Is it possible to have a default application.yml in a custom Spring boot starter?

I am facing an issue with my custom spring boot starter and a spring boot app consumer that uses as a dependency.我的自定义 Spring Boot 启动器和用作依赖项的 Spring Boot 应用程序使用者存在问题。 I have in both an application.yml but it seems that the configuration I am looking for it is only pressent if it is defined in the consumer.我在两个 application.yml 中都有,但似乎我正在寻找的配置只有在消费者中定义时才有意义。

My config in the starter is like this:我在启动器中的配置是这样的:

@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "security")
public class StarterSecurityConfig {
    private boolean jwtEnabled;
    private String[] unsecuredPaths;
    private String[] securedPaths;
}

And I have this bean defined in the AutoConfiguration class:我在 AutoConfiguration 类中定义了这个 bean:

@Bean
public StarterSecurityConfig starterSecurityConfig() {
    return new StarterSecurityConfig();
}

It is perfectly retrieved by the consumer which has this application.yml and another variables:它由具有此 application.yml 和另一个变量的使用者完美检索:

    security:
  jwt-enabled: true
  secured-paths:
    - /user/**
  unsecured-paths:
    - /**

But if I remove that from the consumer and I put it in the application.yml of the starter, the starter beans does not have these properties when creating them.但是,如果我从使用者中删除它并将其放在 starter 的 application.yml 中,则 starter beans 在创建它们时没有这些属性。

Maybe am I missing something?也许我错过了什么?

If I understood properly your issue, I have faced such problem just last week ... I was inspecting this issue and I have some findings (they are not supported by official documentation): if you add dependency and want to use its resources, you have a situation when both application.yml files have the same location - classpath:application.yml , and or they cannot be loaded together, or one of them is overridden by other.如果我正确理解你的问题,我上周就遇到过这样的问题......我正在检查这个问题并且我有一些发现(官方文档不支持它们):如果你添加依赖并想要使用它的资源,你有一种情况,即两个 application.yml 文件具有相同的位置 - classpath:application.yml ,或者它们不能一起加载,或者其中一个被另一个覆盖。 In any case, in my application, it did not work.无论如何,在我的应用程序中,它不起作用。

The straight and simple solution if you just need to load configuration from dependent config file - rename it and load in a possible way (manual loading from YAML, property source's initializer, etc.)如果您只需要从依赖的配置文件加载配置,那么直接而简单的解决方案 - 重命名它并以可能的方式加载(从 YAML 手动加载、属性源的初始值设定项等)

But if this config file should be used anywhere, we can load properties manually in the context.但是如果这个配置文件应该在任何地方使用,我们可以在上下文中手动加载属性。 In a dependency (consumer in your case) create another configuration file, eg consumer-application.yml and next bean in @configuration class:在依赖项(在您的情况下为消费者)中创建另一个配置文件,例如 consumer-application.yml 和 @configuration 类中的下一个 bean:

@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
    var propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    var yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
    yamlPropertiesFactoryBean.setResources(new ClassPathResource("consumer-application.yaml"));
    propertySourcesPlaceholderConfigurer.setProperties(yamlPropertiesFactoryBean.getObject());
    return propertySourcesPlaceholderConfigurer;
}

And you can use properties from YAML-file in both applications with @Value.并且您可以在带有@Value 的两个应用程序中使用来自 YAML 文件的属性。

But the simplest way - to use properties configs.但最简单的方法 - 使用属性配置。 In that case, you can just set @PropertySource("classpath:consumer-application.properties") in consumer and @PropertySource(value = {"classpath:application.properties", "classpath:consumer-application.properties"}) In my case both variants work correctly.在这种情况下,您可以在消费者和@PropertySource(value = {"classpath:application.properties", "classpath:consumer-application.properties"})设置@PropertySource("classpath:consumer-application.properties") @PropertySource(value = {"classpath:application.properties", "classpath:consumer-application.properties"})在我的情况两种变体都能正常工作。

You can try initializing the member variables on the starter itself.您可以尝试在 starter 本身上初始化成员变量。 If consumer wants to override the values they can do it with they're application configuration.如果消费者想要覆盖这些值,他们可以使用应用程序配置来实现。

@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "security")
public class StarterSecurityConfig {
    private boolean jwtEnabled = true;
    private String[] unsecuredPaths = { "/user/**" };
    private String[] securedPaths = { "/**" };
}

Fews more ideas:更多的想法:

I would make jwtEnabled as false and would remove the @Configuration and @ConfigurationProperties from the above Class and create an SecurityAutoConfiguration Class with other beans.我会将 jwtEnabled 设为 false,并从上面的类中删除 @Configuration 和 @ConfigurationProperties 并使用其他 bean 创建一个 SecurityAutoConfiguration 类。

@Configuration
public class SecurityAutoConfiguration{

   @Bean
   @ConfigurationProperties(prefix = "security")
   public StarterSecurityConfig starterSecurityConfig(){
     return new StarterSecurityConfig();
   }

   @Bean
   @ConditionalOnProperty(value="security.jwtEnabled", havingValue = "true")
   public JwtService jwtService(StarterSecurityConfig starterSecurityConfig) {
     return new JwtService(starterSecurityConfig);
   }   

}

the consumers will be able to enable or disable the security-starter with their application configuration using security.jwtEnabled flag.消费者将能够使用 security.jwtEnabled 标志通过他们的应用程序配置启用或禁用 security-starter。

暂无
暂无

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

相关问题 在spring.boot中覆盖application.yml文件中的默认值 - override default values in application.yml file spring boot Java Spring 引导自定义属性在 application.yml 中不起作用 - Java Spring Boot custom properties dont work in application.yml Spring Boot application.yml 和 @Value 不起作用 - Spring Boot application.yml and @Value is not working spring 启动应用程序.yml 中的环境变量 - Environment variable in spring boot application.yml spring-boot-starter-data-mongodb-reactive 从 application.yml 设置密钥库密码以使用 X509 进行连接 - spring-boot-starter-data-mongodb-reactive setting keystore password from application.yml for connecting using X509 是在 Spring Boot application.yml 中为具有默认值的列表定义变量的方法 - Is where a way to define a variable in the Spring Boot application.yml for a list with a default value 如何在Spring Boot中指定自定义运行状况检查注册表(application.yml) - How to specify custom health check registry in spring boot (application.yml) Spring Boot - 如何通过实现 BeforeAllCallback 的自定义扩展类设置或覆盖 application.yml 中定义的属性? - Spring Boot - How to set or override properties defined in application.yml via a custom extension class that implements BeforeAllCallback? Spring boot application.yml 中的 Spring Kafka SSL 设置 - Spring Kafka SSL setup in Spring boot application.yml Spring Boot - 从 application.yml 和 application.properties 注入 - Spring Boot - inject from application.yml and application.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM