最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Access parent config properties when autowiring child properties - Stack Overflow

programmeradmin0浏览0评论

I have a java springboot application I have application.yaml in which I have a section as below where timeOut and retryCount along with couple more (not shown here) will be common for children myEventhub1 and someEventHub

azure:
  eventhub:
    timeOut: ${timeOut}
    retryCount: ${retryCount}
    myEventhub1:
      eventhubName: ${myEventhubName}
      sasKeyName: $(myEventhubKeyName}
      sasKeyValue: $(myEventhubKey}
    someEventHub: 
      eventhubName: ${someEventhubName}
      sasKeyName: $(someEventhubKeyName}
      sasKeyValue: $(someEventhubKey}

I have a class (it is from a reference library I cannot change)

   public class EventhubSettings {
        private String eventhubName;
        private String sasKeyName;
        private String sasKeyValue;
        private int retryCount;
        private int timeOut;
    }

I have a class as below

    @Getter
    @Setter
    @Configuration
    @ConfigurationProperties("azure.eventhub")
    public class MyEventhubConfig {
        private EventhubSettings myEventhub1;
        private EventhubSettings someEventHub;
    }

How can I set the properties retryCount and timeOut in my Eventhub1 and someEventHub in the class MyEventhubConfig. Would it be possible to inherity parent level properties like retryCount and timeOut into myEventhub1 and someEventHub

Only way I could get the autowire was if I defined retryCount and timeOut under myEventhub1 and someEventHub in application.yaml settings instead of defining them at their parent level

I have a java springboot application I have application.yaml in which I have a section as below where timeOut and retryCount along with couple more (not shown here) will be common for children myEventhub1 and someEventHub

azure:
  eventhub:
    timeOut: ${timeOut}
    retryCount: ${retryCount}
    myEventhub1:
      eventhubName: ${myEventhubName}
      sasKeyName: $(myEventhubKeyName}
      sasKeyValue: $(myEventhubKey}
    someEventHub: 
      eventhubName: ${someEventhubName}
      sasKeyName: $(someEventhubKeyName}
      sasKeyValue: $(someEventhubKey}

I have a class (it is from a reference library I cannot change)

   public class EventhubSettings {
        private String eventhubName;
        private String sasKeyName;
        private String sasKeyValue;
        private int retryCount;
        private int timeOut;
    }

I have a class as below

    @Getter
    @Setter
    @Configuration
    @ConfigurationProperties("azure.eventhub")
    public class MyEventhubConfig {
        private EventhubSettings myEventhub1;
        private EventhubSettings someEventHub;
    }

How can I set the properties retryCount and timeOut in my Eventhub1 and someEventHub in the class MyEventhubConfig. Would it be possible to inherity parent level properties like retryCount and timeOut into myEventhub1 and someEventHub

Only way I could get the autowire was if I defined retryCount and timeOut under myEventhub1 and someEventHub in application.yaml settings instead of defining them at their parent level

Share Improve this question edited Feb 1 at 9:10 dan1st 16.5k17 gold badges98 silver badges131 bronze badges asked Feb 1 at 9:09 user29454562user29454562 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

The properties nesting level is more like a composition rather than an inheritance so there's no parent or child concepts. If you want to have a property, let's say, retryCount in some (or all levels) of nesting, and the precedence to be taken from this to the outer level, probably you can do it manually.

This means, overriding the getter for this particular property to look for the current value, but if not set, to go back to the outer (or parent as you described it). But then you need to keep track of this parent.

interface RetryableConfig {

    RetryableConfig getOuterConfig();

    void setOuterConfig(RetryableConfig outer);

    default int getRetryCount();
}
@Getter @Setter
abstract class RetryableConfigAbstract implements RetryableConfig {

    protected RetryableConfig outerConfig;
    private Integer retryCount;

    public int getRetryCount() {
        if (retryCount != null) 
           return retryCount;
        return outerConfig.getRetryCount();
    }
}

In your actual config classes, you need to set the outer once getting the child:

@Setter
@Configuration
@ConfigurationProperties("azure.eventhub")
public class MyEventhubConfig extends RetryableConfigAbstract {
    private EventhubSettings myEventhub1;
    private EventhubSettings someEventHub;

    public EventhubSettings getMyEventhub1() {
        if (myEventHub1 != null) myEventHub.setOuter(this);
        return myEventHub1;
    }

    // same for `someEventHub`

}
public class EventhubSettings extends RetryableConfigAbstract {
    private String eventhubName;
    private String sasKeyName;
    private String sasKeyValue;
    private int retryCount;
    private int timeOut;
}

In this case, when you invoke:

myEventHub.getMyEventhub1().getRetryCount();

It will actually set the outer variable, so the inner getRetryCount() will check for the inner existence, if not, will delegate to parent. Since the implementation of the parents getRetryCount() has the same code, it may also delegate to parent if any (achieving an infinite level of nesting if necessary).

发布评论

评论列表(0)

  1. 暂无评论