Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to Scan Packages for JAXB Marshalling (java example provided) [SPR-8986] #13626

Closed
spring-projects-issues opened this issue Jan 3, 2012 · 9 comments
Assignees
Labels
in: data type: enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Jan 3, 2012

David Harrigan opened SPR-8986 and commented

Hi,

In our application we have a rapidly growing number of JAXB2 annotated classes. It is a right pain to add these classes manually to the "classesToBeBound" property in the Jaxb2Marshaller. Given that other components (I'm looking at you Hibernate : AnnotationSessionFactoryBean) have the ability to automatically add classes from packages that match annotations, why not then for the Jaxb2Marshaller (having to key in the classes manually is so old skool).

I've extended Jaxb2Marshaller (file attached) that scans on the classpath for appropriately annotated classes. Please do review and I hope that this can be incorporated into the next release. I'm happy to make changes to the codebase if required to bring it up to Spring coding standards.

It's a pity that afterPropertiesSet is marked as Final in Jaxb2Marshaller since I can't override that method to set up the setClassesToBeBound before then calling the super afterPropertiesSet. Currently as the code stands, I have to provide a dummy setClassesToBeBound and setLazyInit to be true. This dummy is then replaced by overriding the getJaxbContext. I think this needs rewriting.

An example of use:

<bean id="marshaller" class="foo.bar.AnnotationJaxb2Marshaller">
    <property name="lazyInit" value="true" />
    <property name="classesToBeBound">
       <list>
           <value>foo.bar.Class</value>
       </list>
    </property>
    <property name="packagesToScan">
        <list>
            <value>foo.bar.jaxb.model</value>
        </list>
    </property>
</bean>

-=david=-


Affects: 3.1 GA

Attachments:

Issue Links:

  • #13835 HTTP response code 308 ("is duplicated by")
  • #13844 Add ClasspathScanningJaxb2Marshaller for spring OXM ("is duplicated by")

Referenced from: commits 8980ce7, 79f32c7, ff9ad7a

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 3, 2012

David Harrigan commented

Oops, sorry, lazyInit is not required. I misread the code :-) However, the classesToBeBound is required still. Just a dummy value to get round the check.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 4, 2012

David Harrigan commented

Hi,

A fix for the getJaxbContext - this time, just ignore the classesToBeBound if packagesToScan is not empty.

Also make a defensive copy of the packagesToScan (good programming practice.)

-=david=-

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 7, 2012

David Harrigan commented

Hi,

I've created a pull request on GitHub with proposed code changes to the Jaxb2Marshaller to enable package scanning.

#15

-=david=-

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 10, 2012

Arjen Poutsma commented

Hey,

What's the advantage of this feature compared to the contextPath property that's already there? For instance, the example you give could also be:

<bean id="marshaller" class="foo.bar.AnnotationJaxb2Marshaller">
  <property name="contextPath" value="foo.bar.jaxb.model" />
</bean>

or you could use the contextPaths property if you have multiple packages.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 10, 2012

David Harrigan commented

Oops, sorry for the formatting, forgot the appropriate keywords:

Hi,

Thanks for your comments Arjen, here are my replies:

Setting the ContextPath ultimately calls newInstance on JAXBContext. According to the JAXB documentation

JAXBContext from Java6 JavaDoc

each package has to be explicitly set by the programmer. The advantage of my approach is that I use the same approach as the AnnotationSessionFactoryBean used in the ORM package to automatically scan all packages and sub packages to help the programmer out. I can simply set a top level package and let Spring do the work for me instead of me listing every package and sub package manually.

Again, according to the JAXBContext documentation, even if I do set the packages using the contextPath, it must follow these constraints:

Every package listed on the contextPath must meet one or both of the following conditions otherwise a JAXBException will be thrown:
it must contain ObjectFactory.class
it must contain jaxb.index

Naturally, I don't want to create a jaxb.index file in each package since using Annotations should be sufficient. Reading the javadoc, it appears that if I fail to meet ONE or BOTH of the conditions, then a JAXBException will be thrown. The beauty of my approach is that you don't need to do that, I collate together a list of annotated classes and call the newInstance method on JAXBContext that uses an Array of Classes - just like the classesToBeBound method does.

Thank you Arjen, look forward to you reply

=david=

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 20, 2012

David Harrigan commented

Hi Arjen,

How's it going? Do you think my reply to be reasonable in that by providing package scanning for JAXB annotated classes to be another valid approach to building the JAXBContext?

I recongise that there would be work for the XSD to do - but I'm not great at that :-)

-=david=-

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 20, 2012

Arjen Poutsma commented

Hey David,

I'm currently working on it...

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 20, 2012

David Harrigan commented

Hi Arjen,

That is great news :-) If I can be of any help at all, please do ask :-)

-=david=-

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 20, 2012

Arjen Poutsma commented

Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants