从子类访问父字段配置属性

发布于 2025-01-17 22:31:22 字数 742 浏览 0 评论 0原文

我有以下课程:

parent.class

@Configuration
public class Parent{
    boolean foo;
}

child.class

@Configuration
@Configuration(prefix = "text.prop")
public class Child extends Parent{
    int num;
}

child2.class

@Configuration
@Configuration(prefix = "text.prop2")
public class Child2 extends Parent{
    int num;
}

application.yml:

text:
    prop:
        foo: true
        num: 67
    prop2:
        foo: false
        num: 67

当我自动向child.class.class时,字段foo 不是从application.yml

是否有没有更好的方法来初始化?或当弹簧初始化child.class时,我如何访问字段foo

I have the following classes:

Parent.class

@Configuration
public class Parent{
    boolean foo;
}

Child.class

@Configuration
@Configuration(prefix = "text.prop")
public class Child extends Parent{
    int num;
}

Child2.class

@Configuration
@Configuration(prefix = "text.prop2")
public class Child2 extends Parent{
    int num;
}

application.yml:

text:
    prop:
        foo: true
        num: 67
    prop2:
        foo: false
        num: 67

when I'm auto-wiring the Child.class, the value of the field foo is not getting initialized from application.yml

Is there any better way to do this? or how can i access the field foo value when the spring initializes Child.class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随遇而安 2025-01-24 22:31:22

我进行了以下测试,看起来它有效:

public class Parent {
    private boolean foo;

    public boolean isFoo() {
        return foo;
    }

    public Parent setFoo(boolean foo) {
        this.foo = foo;
        return this;
    }
}

和子类:

@Configuration
@ConfigurationProperties(prefix = "text.prop")
public class Child extends Parent{
    private int num;

    public int getNum() {
        return num;
    }

    public Child setNum(int num) {
        this.num = num;
        return this;
    }

    @Bean
    public String dummyBean(){
        System.out.println(num);
        System.out.println(isFoo());
        return "dummyBean";
    }
}

对于Child2,我们有:

@Configuration
@ConfigurationProperties(prefix = "text.prop2")
public class Child2 extends Parent{
    private int num;

    public int getNum() {
        return num;
    }

    public Child2 setNum(int num) {
        this.num = num;
        return this;
    }

    @Bean
    public String dummyBean2(){
        System.out.println(num);
        System.out.println(isFoo());
        return "dummyBean";
    }
}

在控制台中打印的值:

67
true
67
false

如果parent类是@configuration class添加@AutoconFigurEatter(value = {parent.class})到子类

I did the following test and it looks it works:

public class Parent {
    private boolean foo;

    public boolean isFoo() {
        return foo;
    }

    public Parent setFoo(boolean foo) {
        this.foo = foo;
        return this;
    }
}

and the child class:

@Configuration
@ConfigurationProperties(prefix = "text.prop")
public class Child extends Parent{
    private int num;

    public int getNum() {
        return num;
    }

    public Child setNum(int num) {
        this.num = num;
        return this;
    }

    @Bean
    public String dummyBean(){
        System.out.println(num);
        System.out.println(isFoo());
        return "dummyBean";
    }
}

and for child2 we have:

@Configuration
@ConfigurationProperties(prefix = "text.prop2")
public class Child2 extends Parent{
    private int num;

    public int getNum() {
        return num;
    }

    public Child2 setNum(int num) {
        this.num = num;
        return this;
    }

    @Bean
    public String dummyBean2(){
        System.out.println(num);
        System.out.println(isFoo());
        return "dummyBean";
    }
}

the value printed in console:

67
true
67
false

If Parent class is a @Configuration class add @AutoConfigureAfter(value = {Parent.class}) to child classes

物价感观 2025-01-24 22:31:22

为此,您需要在父类中定义Getters和Setter。

For this to work, you need to define getters and setters in the parent class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文