Announcement Announcement Module
No announcement yet.
Inject BeanFactory? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Two things:
    1) I hard coded the path to the applicationContext.xml b/c I am having trouble getting Spring to find it otherwise (

    2) How can I configure the factory in the applicationContext.xml instead of in the code?


  • #2
    Please, check the reference documentation.


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


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