开发者

Spring constructor injection error

I am getting the following error for a bean in my application context:

Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'businessLogicContext' d efined in class path resource [activemq-jms-consumer.xml]: Unsatisfied dependency expressed through constructor argument  with index 0 of type [java.lang.String]: Could not convert constructor argument value of type [java.util.ArrayList] to required type [java.lang.String]: Failed to convert value of type [java.util.ArrayList] to required type [java.lang.Stri ng]; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.util.ArrayList] to requi red type [java.lang.String]: no matching editors or conversion strat开发者_开发百科egy found
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:53 4)
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:18 6)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAuto wireCapableBeanFactory.java:855)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutow ireCapableBeanFactory.java:765)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCap ableBeanFactory.java:412)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBea nFactory.java:383)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapab leBeanFactory.java:353)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:245)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegis try.java:169)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:242)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListable BeanFactory.java:400)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplic ationContext.java:736)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:369)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java :123)
        at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java :66)

Here is my bean:

<bean id="businessLogicContext" class="org.springframework.context.support.ClassPathXmlApplicationContext" depends-on="resolveProperty">
         <constructor-arg index="0">
            <list>
               <value>jms-applicationContext.xml</value>
               <value>jms-managerBeanContext.xml</value>
               <value>jms-daoContext.xml</value>
               <value>jms-serviceContext.xml</value>
          </list>       
         </constructor-arg>
    </bean>

I don't know what's wrong, I have googled how to inject a string array via constructor injection and the way I do it above seems okay.


The constructor expects a String or String[] type and you're attempting to inject an ArrayList.


Make it like this

<bean id="businessLogicContext" class="org.springframework.context.support.ClassPathXmlApplicationContext" depends-on="resolveProperty">
         <constructor-arg>
            <list>
               <value>jms-applicationContext.xml</value>
               <value>jms-managerBeanContext.xml</value>
               <value>jms-daoContext.xml</value>
               <value>jms-serviceContext.xml</value>
          </list>       
         </constructor-arg>
    </bean>    

Check the constructor doc It accepts array of String, which can be injected using <list>


You can try this:

<bean id="businessLogicContext" class="org.springframework.context.support.ClassPathXmlApplicationContext" depends-on="resolveProperty">
   <constructor-arg value="jms-applicationContext.xml,jms-managerBeanContext.xml,jms-daoContext.xml,jms-serviceContext.xml"/>
</bean>


I accidentally overwrote a certain jar --- xbean.jar with a different version from the one I was previously using.

So when I compiled my jar with this different version as dependency and ran it it was giving me this error. It's working now. hehe.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜