可以在春季扩展pojo类吗? [英] Is it possible to extend a pojo class in Spring?
问题描述
我有一个pojo,其中包含一组私有变量及其设置器和获取器.这个pojo在我的应用程序中被广泛使用.现在,我必须支持一个场景,其中存在多个pojo,每个pojo是我拥有的初始pojo的超集.我是否可以扩展我的原始pojo,以便不需要更改现有的业务逻辑. 我是春天的新手,我不知道这是否有可能. Pojo B应该包含pojo A中的所有内容以及更多其他内容. 在代码内部,我将通过pojo A创建pojo B对象. 基本上,有些事情类似于继承,但带有pojos.
I have a pojo which contains a set of private variables and their setters and getters. This pojo is used extensively across my application. Now, I have to support a scenario where there are multiple pojos and each pojo is a superset of the initial pojo I have. Is it possible that I can extend my original pojo so that I need not change my existing business logic. I am new to spring and I dont know if this is possible. Pojo B should contain everything in pojo A and few more things. Inside the code I will create pojo B objects through pojo A. Basically, some thing similar to inheritance, but with pojos.
推荐答案
通常,您将聚合或继承.
Typically, you would either aggregate or inherit.
继承:
class A {
private String name;
public String getName() {
return name;
}
}
class B extends A {
}
汇总
public interface A {
public String getName();
}
public class AImpl implements A {
private String name;
public String getName() {
return name;
}
}
public class BImpl implements A {
private A wrapped;
public BImpl(A wrapped) {
this.wrapped = wrapped;
}
public String getName() {
return wrapped.getName();
}
}
这篇关于可以在春季扩展pojo类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!