Results 1 to 5 of 5

Thread: how many times can i create ApplicationContext

  1. #1
    Join Date
    Jul 2006
    Posts
    154

    Default how many times can i create ApplicationContext

    hi..

    i have small application i'm testing from within eclipse, i there am using the following:

    public class SpringBeanFactoryUtil {
    public static ApplicationContext getContext(){
    return new FileSystemXmlApplicationContext("spring-config.xml");
    }
    }

    and i'm using that class to get my applicationContext, so in case when i move to websphere or web container , i could simply change the FileSystemXmlApplicationContext to Something else, meanwhile i'm testing my classes using a main method which launches them.


    problem is that i'm wondering what if have a class which does not have access to the application context i created? i once tried to create the ApplicationContext from within a constructor (which itself was being called by an applicationContext), eclipse somehow went into recrusive call.

    do i have to pass a reference to same a single ApplicationContext i first created?

  2. #2
    Join Date
    Dec 2005
    Posts
    269

    Default

    >>do i have to pass a reference to same a single ApplicationContext i first created?

    that's what is recommended. Otherwise there's no much sense in having a structure of fancy singletons, while the context itself is created and destroyed upon each call

  3. #3
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Even better is, to wire everything inside the context and have only one point of initialization. So there is no need to pass around the context itself.

    Regards,
    Andreas

  4. #4
    Join Date
    Jul 2006
    Posts
    154

    Default

    what do you mean one point initialization? and

    let's say i have 3 classes, in a class graph, where the first class creates the 2nd class tne and 2nd class creates the 3rd class, usually, with plain java, i won't be able to create the 3rd class but only from within the 2nd class.

  5. #5
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    The principle of dependency injection is, that you do not create one instance from another but wire everything from the outside. So you create independent instances of class 1, class 2 and class 3 and wire them together. Ideally, these classes know each other only by interfaces, so the concrete implementation class is transparatent to your code which makes replacing it (e.g. for testing) a lot easier.

    Regards,
    Andreas

Posting Permissions

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