Announcement Announcement Module
Collapse
No announcement yet.
how many times can i create ApplicationContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    >>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

    Comment


    • #3
      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

      Comment


      • #4
        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.

        Comment


        • #5
          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

          Comment

          Working...
          X