0

I need to load the applicationcontext in java class in which the applicationcontextaware bean is defined. I need to access the other beans inside the applicationcontext.xml using the applicationcontextaware. I dont want to load the context using

ClassPathXmlApplicationContext("applicationContext.xml");

I need to access the beans inside the applicationContext like this

ApplicationContextAccess.getInstance().getApplicationContext.getbean("BeanName");

Applicationcontextacess implemented as singleton class:

public class ApplicationContextAccess implements ApplicationContextAware {

private ApplicationContext applicationContext = null;
private static ApplicationContextAccess applicationContextAccess=null;

private ApplicationContextAccessor() {

}

public static synchronized ApplicationContextAccess getInstance() {

        if(applicationContextAccess == null)
    {
        applicationContextAccess = new ApplicationContextAccess();
    }

    return applicationContextAccess;

}
public void ApplicationContext getApplicationContext() {
    return applicationContext;
}

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    applicationContext = applicationContext;

}

}

I need to access the beans inside the applicationContext like this ApplicationContextAccess.getInstance().getApplicationContext.getbean("BeanName");

But I have a doubt how the getApplicationContext loads the applicationContext.xml........?

Jessie
  • 963
  • 5
  • 16
  • 26
  • what is the use of factory-method="getInstance"? – Jessie Apr 12 '12 at 18:33
  • You answered your question. Factory-method is the static method used by Spring instead of constructor. So I think your code should work. – Alexey Berezkin Apr 12 '12 at 18:48
  • Thank you for your answer. I have a doubt. whether using like the code mentioned below will create a object for us without using the new ApplicationContextAccess as mentioned in above code. public static synchronized ApplicationContextAccess getInstance() { return applicationContextAccess; } – Jessie Apr 12 '12 at 19:12
  • ``getInstance()`` method as given in the question is ok. – Alexey Berezkin Apr 12 '12 at 19:17
  • Thank you. I have another ques. how the getApplicationContext() works?......from where it loads the applicationContext.xml into it? whether we need to set it using setApplicationContext method or is there some other way it loads? – Jessie Apr 12 '12 at 19:35
  • It's better for you to read Spring reference, but very brief explanation: 1) Spring loads app context from ``applicationContext.xml``. 2) When ``ApplicationContextAccess`` is being loaded, Spring looks that factory method ``getInstance()`` should be used instread of constructor and invokes it to get the bean, thus singleton is also instantiated. 3) Spring also notices that this bean is ``ApplicationContextAware`` and therefore sets app context to it using ``setApplicationContext()``. – Alexey Berezkin Apr 12 '12 at 19:56

0 Answers0