【发布时间】:2022-10-14 14:32:55
【问题描述】:
我正在尝试使用嵌套属性将应用程序属性绑定到下面的类,但它不起作用,因为我获得了嵌套属性的 NPE。基本上这两个嵌套类共享相同的道具,因此想将其提取到自己的类中。
我想访问像helloPropsInstance.foo.getA() 这样的道具,这可能吗?我也在使用龙目岛。
@ConfigurationProperties(prefix="hello")
@ConstructorBinding
@Getter
@Setter
public class HelloProps {
Foo foo;
Bar bar;
public static class Foo extends Base {}
public static class Bar extends Base {}
public static class Base {
private String a; // works fine if I copy these props to Foo and Bar
private String b;
...
}
}
【问题讨论】:
-
删除
static修饰符。 -
不要删除
static,但请告诉我们您从哪里获得 NPE(foo或foo.a),并请确认您有 getter/setter在Base(您只在顶级课程上展示了它们)。 -
@chrylis-cautiouslyoptimistic- npe 在 foo 上。我在 Base 上有 getter 和 setter
-
如果不添加任何新属性,是否有理由不简单地说
Base foo = new Base()? -
目标是为每个班级添加一些额外的道具
标签: java spring-boot lombok