Results 1 to 3 of 3

Thread: Access ApplicationContext loaded by Listener

  1. #1
    Join Date
    Jun 2005
    Location
    Italy
    Posts
    16

    Default Access ApplicationContext loaded by Listener

    Hi,
    Anyone known how to load the applicationContext accessing to Listener?

    The example: I've a listener that load the application context in web.xml

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <listener>
    <description>ApplicationContextLoaderListener</description>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>

    So, what should i do to access to defined beans into applicationcontext by my application?

    I don't want to access it by servlet!

    Any suggest?

  2. #2
    Join Date
    Aug 2004
    Location
    Carlisle, UK
    Posts
    184

    Default

    Have a look at WebApplicationContextUtils.getWebApplicationContex t(ServletContext sc)
    Chris Harris
    Carlisle, UK

  3. #3
    Join Date
    Apr 2009
    Posts
    1

    Default

    If can't or don't want to use the servlet context, you could add a "ProxyContextLoaderListener" to your application:

    Code:
    package demo.spring
    
    import javax.servlet.ServletContextEvent;
    import org.springframework.web.context.ContextLoader;
    import org.springframework.web.context.ContextLoaderListener;
    import org.springframework.web.context.WebApplicationContext;
    
    public class ProxyContextLoaderListener extends ContextLoaderListener {
    	private static WebApplicationContext ctx;
    
    	@Override
    	public void contextInitialized(ServletContextEvent event) {
    		super.contextInitialized(event);
    		ctx = ContextLoader.getCurrentWebApplicationContext();
    	}
    
    	@Override
    	public void contextDestroyed(ServletContextEvent event) {
    		super.contextDestroyed(event);
    		ctx = null;
    	}
    
    	public static WebApplicationContext getCtx() {
    		return ctx;
    	}
    }
    The code that needs to access a bean created by ApplicationContext could be as follows:

    Code:
            ...
            MyClass cl = (MyClass) ProxyContextLoaderListener.getCtx().getBean(
    				"myBeanId");
            ...
    In the web.xml file you specify the ProxyContextLoaderListener instead of the standard Spring ContextLoaderListener:

    Code:
         ...
        <listener>
            <listener-class>
            	demo.spring.ProxyContextLoaderListener
            </listener-class>
        </listener>
        ...

Similar Threads

  1. Replies: 4
    Last Post: Jun 4th, 2011, 11:56 AM
  2. Replies: 2
    Last Post: Oct 13th, 2005, 09:58 AM
  3. Replies: 11
    Last Post: Jul 19th, 2005, 10:24 PM
  4. Replies: 6
    Last Post: Jun 1st, 2005, 04:57 PM
  5. Replies: 2
    Last Post: Apr 2nd, 2005, 11:44 PM

Posting Permissions

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