【发布时间】:2016-09-06 12:36:54
【问题描述】:
我们如何将以下对象转换为 spring bean 以便我们可以保留确切的功能?通过功能,我试图专注于构造函数初始化。
class RealObject {
private String f1;
private String f2;
private String f3;
private int f4;
private int f5;
public RealObject(String f1, String f2, String f3, int f4, int f5) {
this.f1 = f1;
this.f2 = f2;
this.f3 = f3;
this.f4 = f4;
this.f5 = f5;
}
... getters ...
}
spring bean 的样子
@Bean
class RealObject {
private String f1;
private String f2;
private String f3;
private int f4;
private int f5;
... getters ...
}
这个 bean 的问题是它永远不会强迫我在我的对象中提供我需要的参数。我可以为每个输入做一些手动检查,但如果你有太多......它根本不会优雅。
我有什么解决方案?
稍后编辑:我需要在运行时传递值。
【问题讨论】:
-
因为您使用 Spring(或任何 DI 解决方案)并不意味着一切都必须是托管 bean。为什么你需要这个类由 spring 管理?