-
Aug 29th, 2008, 06:46 AM
#1
Inject BeanFactory?
I currently load my applicationContext.xml by using a Java class I made:
public final class SpringUtil {
private static final BeanFactory factory;
static {
String path="C:/apache-tomcat-6.0.16/webapps/myapp/WEB-INF/applicationContext.xml";
final Resource resource = new FileSystemResource(path);
factory = new XmlBeanFactory(resource);
}
public static Object getBean(String name) {
factory.getBean(name);
}
}
Two things:
1) I hard coded the path to the applicationContext.xml b/c I am having trouble getting Spring to find it otherwise (http://forum.springframework.org/showthread.php?t=59488)
2) How can I configure the factory in the applicationContext.xml instead of in the code?
Thanks
-
Aug 29th, 2008, 09:07 AM
#2
-
Aug 29th, 2008, 09:49 AM
#3
Ok I can define the bean factory in my applicationContext.xml. Now I'd like to have a Singleton class "SpringUtil" that I'd like my objects to use for pulling in dependencies.
For example I have a Java class that gets created one and a while (based on user action) and it uses an object defined in Spring, so when it gets created it goes to Spring and pulls out the dependency.
I'd prefer to do SpringUtil.getBean(...) instead of having Spring library spread around.
Unless you suggest a way to inject the bean into my Java class when it needs it (this java class is not a singleton).
-
Aug 29th, 2008, 04:52 PM
#4
As suggested before the reference guide and the sample applications are your friends. You don't want to clutter your code with all the references to your utility class pulling in dependencies.
I suggest understand the basics before proceeding further.
You have 2 options.
1) Define the bean you want to create as a prototype
2) Use @Configurable
Next to that you don't want to use a BeanFactory you want to use an ApplicationContext, the differences are explained in chapter 3 of the reference guide.
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