Results 1 to 4 of 4

Thread: Inject BeanFactory?

  1. #1

    Default 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

  2. #2
    Join Date
    Mar 2007
    Posts
    515

    Default

    Please, check the reference documentation.

  3. #3

    Default

    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).

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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