Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8641629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:34:32+00:00 2026-06-12T11:34:32+00:00

The Question I’m new to Java and spring and I’d like to know how

  • 0

The Question

I’m new to Java and spring and I’d like to know how to structure code which marshals different objects from XML and then processes them. I’m converting some code JAVA code from before my time to use spring. I know the way I’ve approached this this probably, wrong but If someone could offer a few pointers on how to restructure things “the Spring way” that would help alot. I’ve read a lot of the Spring docs, but I’m finding it hard to apply what is in there to my code.

The Situation

I’m not going to post the whole code tree as even and simple example is a lot of code (which is the problem). So I’ll just describe the method.

I’ve got XML and schemas for two classes CLASSA & CLASSB. I’ve generated JAVA wrappers using xjc. I’ve got a JAVA class which is a wrapper for the JAXB marshaller. The wrapper needs to be given the class name and the package name of the class to be marshalled on construction.

public JaxbWrapper(String ClassName, String packageName) throws ClassNotFoundException, InstantiationException,
        IllegalAccessException, JAXBException
{

    this.packageName = packageName;

            // set the context is the expensive operation
    setJAXBContext(Class.forName(fullJaxbClassName(ClassName)).newInstance());

            // get the schma from preset schema dir
    schemaFileName = findSchema(ClassName); 
    myMarsheller = jc.createMarshaller(); 
    myMarsheller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
}

I’ve then got one bean for each instance of the JaxBWrapper for both ClassA and ClassB. They are almost identical so only the one for class A is shown.

<bean id="jaxbParser_CLASSA" class="org.test.JAXB.JaxbWrapper"
    lazy-init="true" scope="prototype">
    <constructor-arg index="0" type="java.lang.String"
        value="CLASSA" />
    <constructor-arg index="1" type="java.lang.String"
        ref="testPackage" />
    <property name="encoding" ref="xmlEncoding" />
</bean>

Unhelpfully the XML files I have to process are delivered one at a time in file with a fixed name and a rolling counter (and I cannot change that). So I’m unable to tell what object is in the file from the filename. I’ve have a tiny utility function which check which the object type is in the file I’m trying to process.

I then have the following function:

public static Object loadXML(String xmlFile, String fileName) throws Exception
{
    InputStream iStream = XmlUtils.openFile(xmlFile);
    String xmlObjectType = XmlUtils.getObjectTypeFromXML(xmlFile);
            // get the right JAXB processor
    JaxbWrapper jaxB = myContext.getBean("jaxbParser_" + xmlObjectType , JaxbWrapper.class);
    Object  obj = jaxB.objectFromXML(iStream, xmlObjectType , fileName);
    return obj;
}

So I’m taking the object name and getting the right bean from the context. This means I could use things like Spring’s object pool to hold lots of JaxbWrappers for different objects. However the way I’ve implemented this feels wrong to me. I don’t like the myContext.getBean(“jaxbParser_” + xmlObjectType method of getting the JaxbWrapper bean.

So my question is: Is there a better way of structuring this code? A few simple pointers would be very much appreciated.

Additional Complexity

This is where things, at the moment, become really unmanageable. Once the marshaling stage has completed I’ve got a post processing stage where there are several different post processor for each class type. I’ve got Spring beans for each of these and I’m getting them from the application context using:

myContext.getBean(xmlObjectType + "_" + processorType + "_" + xmlObjectType);

where:

  • xmlObjectType is a string CLASSA or CLASSB which is set by reading the object type from the file (as above).
  • processorType is s string which is set from the comand line.

To set the proceesorType I’m doing something like the following when the application starts.

    if (line.hasOption("file"))
    {
        processorType = "FileProcessor";
    }
    if (line.hasOption("print"))
    {
        processorType = "PrintProcessor";
    }

Again, I don’t think this is the right way to do things, but It’s the best I’ve got at the moment :-(.

I guess the more general question is how do JAXB and Spring work together? In the real world I have lots very large and complex CLASSA and CLASSBs. I’ve got java classes for these generated by xjc. As I’m using xjc I have to use jaxb (I guess). The question is how to do that in the Spring world.

Or can I get rid of JAXB and use a spring component. I’d need to use something else to generate all the classes other than xjc. But I cannot find anything that would perform that task

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:34:34+00:00Added an answer on June 12, 2026 at 11:34 am

    There is a lot of stuff going on here and I have to agree with @BlaiseDoughan you should be able to do this outside of Spring.

    Although Spring does do object pooling (aop) and has some management for throw away objects you should not use it for such. Basically anytime your doing getBean during runtime unless your know what your doing your doing it wrong.

    Spring is for managing behavior based singletons.

    So what you want to do is think how can make a singleton that does the behavior… In your case you want to make a Factory of JaxbWrapper.class (and I don’t mean Spring’s special bean Factory).

    So something like:

    public class JaxbWrapperFactory {
        Map<String, JaxbWrapper> pool = new ConcurrentHashMap(); // recommend Guava's Cache
    
        public JaxbWrapper create(String ClassName, String packageName) throws ClassNotFoundException, InstantiationException { }
    
        public Object loadXML(String xmlFile, String fileName) throws Exception {
         /// use create from above
        }
    
    }
    

    Now in Spring you can only wire JaxbWrapperFactory.

    If you want to make different strategies for create JaxbWrapper based on the input xml you could make sort of strategy interface and wire in the implementations. But I don’t think you need that complexity.

    Basically to boil it down these tips:

    • Be-aware that singletons and most spring managed beans must be thread safe.
    • Reserve public static methods for utility only as this leads to evil singletons.
    • Often times for object creation its best to make your own factory pattern.
    • You might want to take a look at JAXB’s factory support.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question: I receive the following error for the code below, does anyone know why?
Question is not correctly framed i know, how do i code the below logic
Question: I want to populate a LINQ table from code, not DataBase. Is it
Question from Object-Oriented JavaScript book: Imagine Array() doesn't exist and the array literal notation
Question is already been asked in title. Here is a code: (function($){ var filter
Question I'd like the CSS background texture for my content area to begin immediately
question origin Given an unsorted array of size n containing objects with ids of
Question should be self explanatory. I have a datagridview which has a column whose
Question How can I remove empty xml tags in PHP? Example: $value1 = 2;
Question: I want to test an if statement in PostgreSQL: IF (SELECT COUNT(*) FROM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.