-
Jul 11th, 2006, 08:10 AM
#1
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?
-
Jul 11th, 2006, 09:07 AM
#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
-
Jul 11th, 2006, 09:16 AM
#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
-
Jul 11th, 2006, 11:12 AM
#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.
-
Jul 17th, 2006, 02:02 AM
#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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules