Announcement Announcement Module
No announcement yet.
Accessing dataSource from ApplicationContext Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing dataSource from ApplicationContext

    I have a bit of an issue.

    I am running an MDP in websphere consuming incoming MQ messages.

    I load an instance of applicationContext using ContextLoaderListener in my web.xml

    I subsequently need to access a dataSource from within the loaded applicationContext to do this I was using ClassPathXmlApplicationContext. This, I've discovered will load another applicationContext into memory which is a bad thing as this won't shutdown when I close the application - it means I need to bounce the cluster it is on to stop it.

    I have trawled around loads trying to find a way to access the initial applicationContext dataSource I loaded but have been unable to find a method to do this. Can anyone help/point me to a thread and/or javadocs to explain what I need to do.

    As further explanation I require the datasource to create the jdbcTemplate to load the message into a database.


  • #2
    I have now discovered that I can get the context from...

    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContex t(servletContext);

    and then use this. Unfortunately, as I'm not using JSP or anything of that nature I am not sure how to get the servletContext to be able to then get the application context beans.


    • #3

      if you are running inside Spring MDP, then you can inject the datasource or in particular the jdbctemplate inside the bean which receves the message.

      If you really need to fetch the datasource (which i personally don't recommend), then you have to implement the BeanFactoryAware interface. With this you will get the beanfactory, from that you can pull out the datasource.



      • #4
        thank you for your response.

        I do not understand what you mean by inject the datasource?

        Could you point me towards an example of this



        • #5
          Is your MDP a Spring bean?

          Is your MDP defined in your application context? If so, then modify your class by giving it another property called "dataSource", and then set that property through classic IoC injection in the app context. See?


          • #6
            I think I understand now, from yours and Agim's help as well as reading the manual on the IoC container.

            Thanks very much for your help.