Results 1 to 6 of 6

Thread: Accessing dataSource from ApplicationContext

  1. #1

    Default 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.

    Thanks

  2. #2

    Default

    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. #3
    Join Date
    Nov 2006
    Location
    Germany
    Posts
    452

    Default

    Hi,

    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.

    rgds
    agim

  4. #4

    Default

    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

    thanks

  5. #5
    Join Date
    Aug 2006
    Posts
    382

    Default 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?
    Greg L. Turnquist (@gregturn), SpringSource/VMware
    Project Lead: Spring Python and author of Spring Python 1.1 and Python Testing Cookbook.
    Listen to Pond Jumpers, the international podcast for open source developers.
    These comments are my own personal opinions, and do not reflect those of my company.

  6. #6

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •