【问题标题】:Loading string list from application.yml into a component将字符串列表从 application.yml 加载到组件中
【发布时间】:2016-12-01 17:21:28
【问题描述】:

在 stackoverflow 上阅读了几篇 answers 之后。我试图实现它被推荐的方式,但我仍然没有在我的component 中获得字符串列表。

这是我的application.yml 文件

appName:
  db:
    host: localhost
    username: userX
    password: passX
    driverClassName: org.postgresql.Driver
  shipment:
    providers:
      supported1:
        - one
        - two

而我的component 课程是

@Component
@ConfigurationProperties(prefix = "appName.shipment.providers")
public class ProviderUtil {
  List<String> supported1;

  /* This class has other util methods as well */
}

在这里,我期望 supported1 会有字符串列表 onetwo 但它是空的。有人可以帮助这里发生的事情以及如何解决它吗?

【问题讨论】:

    标签: spring dependency-injection configuration spring-boot yaml


    【解决方案1】:

    我关注了这篇文章并为列表添加了 getter 和 setter。它对我有用。

    代码:

    @Service("testservice")
    @ConfigurationProperties(prefix="es")
    public class TestService {
    
         @Value("${url.endPoint}")
         private String endpointUrl;
    
         private List<String> scope;
        public List<String> getScope() {
            return scope;
        }
    
        public void setScope(List<String> scope) {
            this.scope = scope;
        }
    ..
    }
    

    【讨论】:

    • 请不要将所有内容都写在代码块中。对文本使用普通标记。
    【解决方案2】:

    你的类路径有 spring-boot-configuration-processor 依赖吗? 试试看here

    还将您的 bean 从 @Component 更改为 @Configuration

    【讨论】:

    • 是的,我完全按照文档中提到的方式添加了它。将optional 值设置为true 并且我使用component 因为这个响应>> stackoverflow.com/questions/26699385/… ...话虽如此,我已经尝试了configurationcomponent 但没有运气
    • 尝试分享您的代码,看看我们能否以某种方式帮助您。
    • 我发现了这个问题。我没有提供 getter/setter。我已经公开了这个领域,我认为它会起作用
    猜你喜欢
    • 2013-12-28
    • 1970-01-01
    • 2020-04-15
    • 2022-01-13
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-15
    相关资源
    最近更新 更多