Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否有类似于JAXB注释类的后构造?_Java_Dependency Injection_Constructor_Jaxb_Instantiation - Fatal编程技术网

Java 是否有类似于JAXB注释类的后构造?

Java 是否有类似于JAXB注释类的后构造?,java,dependency-injection,constructor,jaxb,instantiation,Java,Dependency Injection,Constructor,Jaxb,Instantiation,JAXB中是否有这样一种功能,可以在类被解组后(即由JAXB构造后)对其执行操作?如果不是,我如何才能做到这一点?这不是100%的解决方案,但您始终可以使用 对于这种类型 缺点是您必须自己序列化类(?)。我不知道有任何访问和调用默认序列化机制的简单方法。但是,通过自定义[XmlAdapter],您可以控制类型的序列化方式以及在序列化之前/之后发生的事情。尽管JAXB中似乎不存在所需的功能,但我还是设法做到了 实现正确方向的目标: 我正在使用JSR-305的@PostConstruct注释 (这

JAXB中是否有这样一种功能,可以在类被解组后(即由JAXB构造后)对其执行操作?如果不是,我如何才能做到这一点?

这不是100%的解决方案,但您始终可以使用 对于这种类型


缺点是您必须自己序列化类(?)。我不知道有任何访问和调用默认序列化机制的简单方法。但是,通过自定义[
XmlAdapter
],您可以控制类型的序列化方式以及在序列化之前/之后发生的事情。

尽管JAXB中似乎不存在所需的功能,但我还是设法做到了 实现正确方向的目标:

  • 我正在使用JSR-305的
    @PostConstruct
    注释
    (这只是一个nacked注释,JSR不提供任何功能)
  • 我向解组器添加了一个解组器侦听器,每次对象解组时JAXB都会调用该侦听器
  • 我使用Java反射检查这个对象,并在方法上搜索
    @PostConstruct
    注释
  • 我执行这个方法
测试。有效。

这是代码。抱歉,我正在使用一些外部反射API来获取所有方法,但我认为这个想法是可以理解的:

实施 提交了功能请求

您可以使用JAXB类中定义的,例如:

// This method is called after all the properties (except IDREF) are unmarshalled for this object, 
// but before this object is set to the parent object.
void afterUnmarshal( Unmarshaller u, Object parent )
{
    System.out.println( "After unmarshal: " + this.state );
}

好问题,但我不认为有这样的事。但是,我鼓励您在以下位置记录功能请求:)尝试在
postConstructMethod
上调用
setAccessible(true)
,这将强制它可调用。
@XmlAccessorType(XmlAccessType.NONE)
public abstract class AbstractKeywordWithProps
    extends KeywordCommand {

  @XmlAnyElement
  protected final List<Element> allElements = new LinkedList<Element>();

  public AbstractKeywordWithProps() {
  }

  @PostConstruct
  public final void postConstruct() {
    // now, that "allElements" were successfully initialized,
    // do something very important with them ;)
  }

}

// further classes can be derived from this one. postConstruct still works!
// This method is called after all the properties (except IDREF) are unmarshalled for this object, 
// but before this object is set to the parent object.
void afterUnmarshal( Unmarshaller u, Object parent )
{
    System.out.println( "After unmarshal: " + this.state );
}