Announcement Announcement Module
Collapse
No announcement yet.
Basic Spring JMS Config question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Basic Spring JMS Config question

    I have a simple bean which has a method to send messages to a queue defined in my spring-jms.xml. In the example code that I have seen and was able to get working, I was getting loading the config via a direct reference in code
    ------------------------------------------------------------------------
    MyBean.java

    Thread.currentThread().setContextClassLoader(
    MessageHelper.class.getClassLoader());

    context = new ClassPathXmlApplicationContext("spring-jms.xml");

    Now I don't want to do this so I put the config in applicationContext.xml like this, but I am unable to get an instance of MyBean, i get the exception (see below)
    ----------------------------------------------------------------------------
    <!-- My Bean -->
    <bean id="myBean" class="com.xxx.MyBean">
    <property name="context" ref="appContext"/>
    </bean>

    <!-- JMS Stuff -->
    <bean id="appContext" class="org.springframework.context.support.ClassPa thXmlApplicationContext">
    <constructor-arg>
    <list>
    <value>spring-jms.xml</value>
    </list>
    </constructor-arg>
    </bean>
    -------------------------------------------------------------------------
    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'myBean' defined in ServletContext resource [/applicationContext.xml]: Cannot resolve reference to bean 'appContext' while setting bean property 'context'; nested exception is org.springframework.beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name 'appContext' defined in ServletContext resource [/applicationContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: There are 0 beans of type [java.lang.String] available for autowiring: []. There should have been exactly 1 to be able to autowire constructor of bean 'appContext'.
    Caused by: org.springframework.beans.factory.UnsatisfiedDepen dencyException: Error creating bean with name 'appContext' defined in ServletContext resource [/applicationContext.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.lang.String]: There are 0 beans of type [java.lang.String] available for autowiring: []. There should have been exactly 1 to be able to autowire constructor of bean 'appContext'.
    at org.springframework.beans.factory.support.Construc torResolver.createArgumentArray(ConstructorResolve r.java:474)
    at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:153)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireConstructor(Abs tractAutowireCapableBeanFactory.java:799)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:717)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:384)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 51)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:156)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:248)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:160)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:261)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:109)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyPropertyValues(Abs tractAutowireCapableBeanFactory.java:1099)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:861)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:421)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 51)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:156)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:248)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:160)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:287)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:352)
    at org.springframework.web.context.ContextLoader.crea teWebApplicationContext(ContextLoader.java:244)
    at org.springframework.web.context.ContextLoader.init WebApplicationContext(ContextLoader.java:187)
    at org.springframework.web.context.ContextLoaderListe ner.contextInitialized(ContextLoaderListener.java: 49)
    at org.apache.catalina.core.StandardContext.listenerS tart(StandardContext.java:3764)
    at org.apache.catalina.core.StandardContext.start(Sta ndardContext.java:4216)
    at org.apache.catalina.core.ContainerBase.addChildInt ernal(ContainerBase.java:760)
    at org.apache.catalina.core.ContainerBase.addChild(Co ntainerBase.java:740)
    at org.apache.catalina.core.StandardHost.addChild(Sta ndardHost.java:544)
    at org.apache.catalina.startup.HostConfig.deployWAR(H ostConfig.java:825)
    at org.apache.catalina.startup.HostConfig.deployWARs( HostConfig.java:714)
    at org.apache.catalina.startup.HostConfig.deployApps( HostConfig.java:490)
    at org.apache.catalina.startup.HostConfig.start(HostC onfig.java:1138)
    at org.apache.catalina.startup.HostConfig.lifecycleEv ent(HostConfig.java:311)
    at org.apache.catalina.util.LifecycleSupport.fireLife cycleEvent(LifecycleSupport.java:120)
    at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1022)
    at org.apache.catalina.core.StandardHost.start(Standa rdHost.java:736)
    at org.apache.catalina.core.ContainerBase.start(Conta inerBase.java:1014)
    at org.apache.catalina.core.StandardEngine.start(Stan dardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(Sta ndardService.java:448)
    at org.apache.catalina.core.StandardServer.start(Stan dardServer.java:700)
    at org.apache.catalina.startup.Catalina.start(Catalin a.java:552)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.apache.catalina.startup.Bootstrap.start(Bootst rap.java:295)
    at org.apache.catalina.startup.Bootstrap.main(Bootstr ap.java:433)
    Sep 26, 2008 5:00:38 PM org.springframework.web.context.ContextLoader initWebApplicationContext

  • #2
    Ehrm WHY?!

    Simply implement ApplicationContextAware, register your bean in the spring-jms.xml and you have access...

    Or even better use Dependency Injection instead of accessing the Context yourself...

    Comment

    Working...
    X