如何将自定义代码添加到 JAXWS 生成的源代码
How to add custom code to JAXWS generated source
是否可以为 JAXWS 自定义代码 class。我想要做的是向生成的 class 添加一些辅助方法,以帮助从中提取信息。我知道我可以子 class 生成的源,但这意味着我必须将它投射到任何我想要信息的地方。
是否可以让 jaxws(或其他工具)将 WSDL 生成的源代码与一些包含我的方法的自定义代码合并在一起? (在 C# 中,我可以使用部分 class 来做到这一点,但 java 似乎没有等效项)...
实现此目的的一种方法是通过方面引入这些辅助方法。 AspectJ would be a potential candidate for this, or if you're using Spring, then Spring AOP 也能胜任。
因此,使用 AspectJ,您将利用称为 "inter-type declaration" 的功能,它允许将新功能引入现有的 classes。如果生成的 JAXWS class 被称为 MyTypeGen
,那么您将使用一个方面(任意命名为 MyTypeAspect
)引入新方法,如下所示:
// generated class
class MyTypeGen {
int x, y;
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
}
// introduce two new methods into MyTypeGen
aspect MyTypeAspect {
public int MyTypeGen.multiply() {
return x * y;
}
public int MyTypeGen.sum() {
return x + y;
}
}
在运行时,您可以调用在构建时编入 MyTypeGen
的这两个新方法。无需铸造。
MyTypeGen mytype = new MyTypeGen();
mytype.setX(3);
mytype.setY(4);
int product = mytype.multiply();
// product = 12
如果您决定改用 Spring AOP,您将创建一个带有 @Aspect
注释的普通 Java class,您将在其中利用introductions 特征。
有很多方法可以做到这一点,其中一种是使用方面,另一个答案已经提到了。
我想建议一个更简单的替代方案,即只扩展生成的 class:
public GeneratedClass {
public void doThis();
}
public ImprovedClass extends GeneratedClass {
public void doExtraStuff();
}
这样做的好处是你对GeneratedClass能做什么有明确的定义。如果您开始将方法编织到 GeneratedClass 中,几年后从事该项目的人可能会认为生成的 class 的所有相似实例都具有相同的方法。或者他们可能会感到困惑,为什么一些生成的 classes 有这些额外的方法,而有些没有。额外的功能应该保留在明确定义的 class 中。只有那个类型的 subclass 可以做额外的事情。这样一来,方法的来源就一目了然了。
我并不是反对代码编织,但我认为代码编织应该只用于不涉及业务逻辑的东西,比如日志、指标,或者用于框架内部。不应将与业务逻辑相关的代码编织到 class.
无论如何,另一种方法是包装 class,但这需要一个接口。
public GeneratedClass implements SomeInterface {
public void doThis();
}
public ImprovedClass implements SomeInterface {
public SomeInterface impl;
public Improvedclass(SomeInterface impl) {
this.impl = impl;
}
public void doThis() {
this.impl.doThis();
}
public void doExtraStuff() {
// the extra stuff
}
}
是否可以为 JAXWS 自定义代码 class。我想要做的是向生成的 class 添加一些辅助方法,以帮助从中提取信息。我知道我可以子 class 生成的源,但这意味着我必须将它投射到任何我想要信息的地方。
是否可以让 jaxws(或其他工具)将 WSDL 生成的源代码与一些包含我的方法的自定义代码合并在一起? (在 C# 中,我可以使用部分 class 来做到这一点,但 java 似乎没有等效项)...
实现此目的的一种方法是通过方面引入这些辅助方法。 AspectJ would be a potential candidate for this, or if you're using Spring, then Spring AOP 也能胜任。
因此,使用 AspectJ,您将利用称为 "inter-type declaration" 的功能,它允许将新功能引入现有的 classes。如果生成的 JAXWS class 被称为 MyTypeGen
,那么您将使用一个方面(任意命名为 MyTypeAspect
)引入新方法,如下所示:
// generated class
class MyTypeGen {
int x, y;
public void setX(int x) { this.x = x; }
public void setY(int y) { this.y = y; }
}
// introduce two new methods into MyTypeGen
aspect MyTypeAspect {
public int MyTypeGen.multiply() {
return x * y;
}
public int MyTypeGen.sum() {
return x + y;
}
}
在运行时,您可以调用在构建时编入 MyTypeGen
的这两个新方法。无需铸造。
MyTypeGen mytype = new MyTypeGen();
mytype.setX(3);
mytype.setY(4);
int product = mytype.multiply();
// product = 12
如果您决定改用 Spring AOP,您将创建一个带有 @Aspect
注释的普通 Java class,您将在其中利用introductions 特征。
有很多方法可以做到这一点,其中一种是使用方面,另一个答案已经提到了。
我想建议一个更简单的替代方案,即只扩展生成的 class:
public GeneratedClass {
public void doThis();
}
public ImprovedClass extends GeneratedClass {
public void doExtraStuff();
}
这样做的好处是你对GeneratedClass能做什么有明确的定义。如果您开始将方法编织到 GeneratedClass 中,几年后从事该项目的人可能会认为生成的 class 的所有相似实例都具有相同的方法。或者他们可能会感到困惑,为什么一些生成的 classes 有这些额外的方法,而有些没有。额外的功能应该保留在明确定义的 class 中。只有那个类型的 subclass 可以做额外的事情。这样一来,方法的来源就一目了然了。
我并不是反对代码编织,但我认为代码编织应该只用于不涉及业务逻辑的东西,比如日志、指标,或者用于框架内部。不应将与业务逻辑相关的代码编织到 class.
无论如何,另一种方法是包装 class,但这需要一个接口。
public GeneratedClass implements SomeInterface {
public void doThis();
}
public ImprovedClass implements SomeInterface {
public SomeInterface impl;
public Improvedclass(SomeInterface impl) {
this.impl = impl;
}
public void doThis() {
this.impl.doThis();
}
public void doExtraStuff() {
// the extra stuff
}
}