从子类访问父字段配置属性
我有以下课程:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我进行了以下测试,看起来它有效:
和子类:
对于Child2,我们有:
在控制台中打印的值:
如果
parent
类是@configuration
class添加@AutoconFigurEatter(value = {parent.class})
到子类I did the following test and it looks it works:
and the child class:
and for child2 we have:
the value printed in console:
If
Parent
class is a@Configuration
class add@AutoConfigureAfter(value = {Parent.class})
to child classes为此,您需要在父类中定义Getters和Setter。
For this to work, you need to define getters and setters in the parent class.