JAXB:如何使用 Spring 自动生成的类?

发布于 2024-10-12 03:39:10 字数 521 浏览 9 评论 0原文

我正在使用 xjcXSD 生成 Java 类。生成的类仅包含集合类型的 getter 方法,例如 List,但不包含 setter 方法。我如何在 Spring Framework 中使用这些生成的类作为 bean,即如何使用 applicationContext.xml 中的数据填充这些字段?

作为对“duffymo”和“fatih”的回复:实际上我可以告诉Spring使用JAXB ObjectFactory及其工厂方法来创建bean,

<bean id="myFactory" class="generated.ObjectFactory" />
<bean id="myBean" factory-bean="myFactory" factory-method="createMyBeanMethod" />

剩下的问题是如何在没有 setter 方法的情况下填充集合字段?

I am using xjc to generated Java classes from an XSD. The resulting classes only include getter methods for collection types, e.g. List, but no setter methods. How can i use these generated classes as beans within the Spring Framework, i.e. how to populate these fields with data from applicationContext.xml?

As a reply to 'duffymo' and 'fatih': Actually i can tell Spring to use the JAXB ObjectFactory and its factory methods to create the beans,

<bean id="myFactory" class="generated.ObjectFactory" />
<bean id="myBean" factory-bean="myFactory" factory-method="createMyBeanMethod" />

the remaining problem is how to populate the collection fields without having setter methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

赠我空喜 2024-10-19 03:39:10

我不相信你可以,因为它们是在 JAXB 的控制下创建的,而不是 Spring bean 工厂。

I don't believe you can, because they were created under JAXB's control and not the Spring bean factory.

岁吢 2024-10-19 03:39:10

您可以使用字段代替 getter/setter。

You can use the field instead of the getter/setter.

小瓶盖 2024-10-19 03:39:10

由于生成的代码处于 JAXB 的控制之下,因此您将无法执行此操作。然而,可能有一个解决方法:

假设 JAXB 为您创建一个 Person 类,生成的属性将受到保护。

您可以创建扩展 Person 类的新类 MyPerson,并将 setter 方法放置在 MyPerson 类中。因此,您可以从 bean 配置文件初始化该属性,并且生成的代码不会覆盖您的更改。它可能是也可能不是适合您的情况的解决方法,因为我不知道详细信息。

     generated :
     public class Person{
          protected List<Something> somethingList;
          .
          .
     }




     class MyPerson extends Person{
         public void setList(List<Something> somethingList){
             this.somethingList= somethingList;
         }

     }

As the generated code is under JAXB's control you wont be able to do that. However, a workaround maybe:

Say JAXB creates a Person class for you, generated properties will be protected.

You can create new class MyPerson extending the Person class and place the setter method inside of MyPerson class. So you can init the property from bean config file and the generated code wont override your changes. It may or may not be a suitable workaround for your case as I dont know the details.

     generated :
     public class Person{
          protected List<Something> somethingList;
          .
          .
     }




     class MyPerson extends Person{
         public void setList(List<Something> somethingList){
             this.somethingList= somethingList;
         }

     }
粉红×色少女 2024-10-19 03:39:10

有一个扩展可以为列表生成设置方法,如下所示:

尽管给定的链接似乎是由于引用的站点已被重新组织而损坏。

值得向 java.net 发布一个问题以获取集合设置器注入器的位置。

There is an extension to generate set methods for lists as indicated here:

Although the given link appears to be broken as the referenced site has been re-organized.

It would be worth posting a question to java.net to get the location of the collection-setter-injector.

北凤男飞 2024-10-19 03:39:10

Instead of populate your xjc-generated beans in applicationContext.xml, you can use Java-based configuration for those beans.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文