对Spring组件的内部组件使用属性

时间:2019-07-05 17:44:18

标签: java spring spring-boot

我有一个组件,这些组件的成员是从YAML文件填充的。我对此组件有一个内部组件,该组件还包含一些来自YAML的成员。

但是当我启动应用程序时,尽管创建了实例,但内部组件的成员并未填充。主要组件成员可以正常加载。

需要知道为什么不从YAML加载Department组件的成员。

更新

我可以看到内部类已加载了成员,但是稍后会在应用程序加载期间完成,但是在此之前,Company组件用于加载其他一些组件。因此,我需要在Company组件初始化之后立即初始化内部类/组件。

这是定义我的组件的方式

@Component
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(Include.NON_NULL)
@ConfigurationProperties(prefix = "company")
public class Company {

    @JsonProperty("connectTimeout")
    private Integer connectTimeout;
    @JsonProperty("socketTimeout")
    private Integer socketTimeout;
    @JsonProperty("clientTimeout")
    private Integer clientTimeout;
    @JsonProperty("dept1")
    @Autowired
    private Department library;
    @JsonProperty("dept2")
    @Autowired
    private Department admin;
    @JsonProperty("dept3")
    @Autowired
    private Department transport;
    @JsonProperty("dept4")
    @Autowired
    private Department finance;

    //Getters and Setters

    @Component
    @SuppressWarnings({"PublicInnerClass", "WeakerAccess"})
    public static class Department {

        @JsonProperty("connectTimeout")
        private Integer connectTimeout;
        @JsonProperty("socketTimeout")
        private Integer socketTimeout;
        @JsonProperty("clientTimeout")
        private Integer clientTimeout;

        //Getters and Setters
    }
}

以下是我的YAML文件

company:
  connectTimeout: 1000
  socketTimeout: 20000
  clientTimeout: 150
  dept1:
    connectTimeout: 100
    socketTimeout: 100
    clientTimeout: 100
  dept2:
    connectTimeout: 100
    socketTimeout: 100
    clientTimeout: 100
  dept3:
    connectTimeout: 100
    socketTimeout: 100
    clientTimeout: 100
  dept4:
    connectTimeout: 100
    socketTimeout: 100
    clientTimeout: 100

1 个答案:

答案 0 :(得分:1)

假设我们拥有以下财产所有人dto的CompanyDepartment;

public class Company {

    private Integer connectTimeout;
    private Integer socketTimeout;
    private Integer clientTimeout;
    private Department library;
    private Department admin;
    private Department transport;
    private Department finance;

    public void setDept1(Department dept1) {
        this.library = dept1;
    }

    public void setDept2(Department dept2) {
        this.admin = dept2;
    }

    public void setDept3(Department dept3) {
        this.transport = dept3;
    }

    public void setDept4(Department dept4) {
        this.finance = dept4;
    }

    // other getter/setters

    public static class Department {

        private Integer connectTimeout;
        private Integer socketTimeout;
        private Integer clientTimeout;

        // getter/setters
    }
}

&只需定义一个包含Bean的配置属性;

@Configuration
public class MyConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "company")
    public Company company() {
        return new Company();
    }
}

使用@JsonProperty不适用于此类属性,因为该注释仅适用于通过Jackson进行反序列化。 @ConfigurationProperties专注于基本的getter / setter,只需添加具有这些名称的setter就足够了,例如setDept1setDept2

详细了解Spring Boot here上的外部化配置