Question

I've got a class which has a static method newInstance(Class[] classes, List properties), and I need Spring to be able to instantiate instances using this class.

What i've got so far in my XML configuration is :

<bean id="jaxbContext" scope="prototype" class="javax.xml.bind.JAXBContext" factory-method="newInstance">
    <constructor-arg>
        <list>
            <value type=">com.company.pas.entity.Partner.class</value>
        </list>
    </constructor-arg>
    <constructor-arg>
        <list>
            <value>com/company/pas/entity/mapping/partner-pojo2xml.xml</value>
        </list>
    </constructor-arg>
</bean>

This throws the below exception. Note that it complains that it can't find the newInstance method, but i assume that is because it can't find a newInstance method with a signature which matches the configuration. I suspect that Spring interprets the list of classes as java.lang.String, and not Class.

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jaxbContext' defined in class path resource [com/company/pas/context/mappingContext.xml]: No matching factory method found: factory method 'newInstance'. Check that a method of the specified name exists and that it is static.
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:500)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:964)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:870)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:479)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:309)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    ... 46 more

How do I get spring to inject an array of Class[] based on a configuration file?

Was it helpful?

Solution

JAXBContext.newInstance() has this overload:

newInstance(String contextPath)
newInstance(String contextPath,ClassLoader classLoader)
newInstance(Class... classesToBeBound)
newInstance(Class[] classesToBeBound,Map<String,?> properties)
newInstance(String contextPath,ClassLoader classLoader,Map<String,?> properties)

To be honest I can't find a newInstance(Class[] classes, List properties) overload so spring is right when tells you it can't find the newInstance() method

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top