The QuestionI'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 SituationI'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 for 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.\[code\]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);}\[/code\]I've then got one bean for each instance of the JaxBWrapper for both ClassA and ClassB. There almost identical so only the one for class A is shown.\[code\]<bean id="jaxbParser_CLASSA" class="org.test.JAXB.JaxbWrapper" lazy-init="true" scope="prototype"> <constructor-arg index="0" type="java.lang.String" value="http://stackoverflow.com/questions/12635203/CLASSA" /> <constructor-arg index="1" type="java.lang.String" ref="testPackage" /> <property name="encoding" ref="xmlEncoding" /></bean>\[/code\]Unhelpfully he 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:\[code\]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;}\[/code\]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 ComplexityThis 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:\[code\]myContext.getBean(xmlObjectType + "_" + processorType + "_" + xmlObjectType);\[/code\]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.