Results 1 to 3 of 3

Thread: Initialisation and getBean within non-EJB Bean

  1. #1
    Join Date
    Apr 2005
    Posts
    5

    Question Initialisation and getBean within non-EJB Bean

    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

  2. #2
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    If you need to create objects at runtime and can't register them as a prototype beans at the IoC container, you should exploit AspectJ weaving. Check corresponding reference section - 6.8.1. Using AspectJ to dependency inject domain objects with Spring

  3. #3
    Join Date
    Sep 2004
    Posts
    602

    Default

    Quote Originally Posted by vichandoko View Post
    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.
    Indeed, this should be done once and once only, usually by a listener in the web.xml for a web app. See http://static.springframework.org/sp...context-create

    What Denis has siad is what you have to do if you can't register them as prototypes (see here http://static.springframework.org/sp...opes-prototype for how to create prototypes), but it may be worth investigating why the architecture of your app is like this anyway, and why you can't use straight singletons.

    Also, do a search in the manual and in the forum for ApplicationContextAware - that may help you out in having beans that at least know about the container.

Posting Permissions

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