I've inherited codes which uses Spring (a very old version of it)
The EJBs extends org.springframework.ejb.support.AbstractStatelessS essionBean and within it, it get the reference to the instance of other bean by using getBean("...") method. Excerpts as follows:
public class Bean2EJB extends AbstractStatelessSessionBean {
private Bean1 bean1;
protected void onEjbCreate() throws CreateException {
bean1 = (Bean1)getBeanFactory().getBean("bean1");
}
...
}
My questions is regarding non-EJB beans. On the codes, the non-EJB are extending org.springframework.beans.factory.InitializingBean , and within it, it gets the reference to the instance of other bean by instantiating a new ApplicationContext and getBean("...") through that context. Excerpts as follows:
public class NonEJBBean extends InitializingBean{
private Bean1 bean1;
public void afterPropertiesSet(){
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("appContext.xml");
bean1 = (Bean1) ctx.getBean("bean1");
}
}
Instantiating the context within the bean feels wrong to me. And through high-level profiling, the instantiation of the ApplicationContext itself is actually rather slow.
What should be the correct way to get reference to a bean within non-EJB?
I can't use declarative settings within XML, because the NonEJBBean is dynamically loaded.
Thanks.
Veny


Reply With Quote
