Announcement Announcement Module
No announcement yet.
ApplicationContext and getBean Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • ApplicationContext and getBean

    I have a application that has 3 beans which are loaded by spring.

    Main class:
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    How do I get a reference to the context in a some other class in the application. If I try to do:
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    in another class then it will reload the context but that is not what I want.

    Please help me.

  • #2
    3.9. Glue code and the evil singleton


    • #3
      I just saw something on BeanFactoryAware.

      would this help? how can I use it?


      • #4
        I don't understand what do you want to do with BeanFactoryAware.

        The reference I mentioned talks that:
        1. You should resolve as much dependencies via Spring as possible. The only need to glue spring-configured and standalone beans is third-party code usage. Make sure you really have that situation at your application;
        2. If it's absolutely necessary to perform the linking with standalone beans, the section I referenced talks about ContextSingletonBeanFactoryLocator and SingletonBeanFactoryLocator;


        • #5
          Yes, you are loading the spring context twice.

          Rather than using getBean you should probably take your app a step further and have Spring to inject the beans into your classes. There is a number of support classes, such as ApplicationContextAware etc. but I don't think this is what you are looking for.




          • #6
            I tried using the locator.
            <bean id="gateway"
            in class A
            ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("beanRefFactory.xml");
            the beans in the applicationContext.xml are not loaded until I do
            BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance();
                    BeanFactoryReference bf = bfl.useBeanFactory("gateway");
            but after doing that or if I try doing bfl.getBean("testbean") then I am getting the following error:
            Exception in thread "main" org.springframework.beans.FatalBeanException:
             Unable to find resource for specified definition. Group resource name [classpath*:beanRefFactory.xml], factory key [gateway]
            what am IK doing wrong here?


            • #7
              1. There is no point in doing
                ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("beanRefFactory.xml");
              2. SingletonBeanFactoryLocator:
                In this implementation, a BeanFactory is built up from one or more XML definition file fragments, accessed as resources. The default resource name searched for is 'classpath*:beanRefFactory.xml'
              3. The classpath*: prefix:
                Classpath*: portability

                The wildcard classpath relies on the getResources() method of the underlying classloader. As most application servers nowadays supply their own classloader implementation, the behavior might differ especially when dealing with jar files. A simple test to check if classpath* works is to use the classloader to load a file from within a jar on the classpath: getClass().getClassLoader().getResources("<someFil eInsideTheJar>"). Try this test with files that have the same name but are placed inside two different locations. In case an inappropriate result is returned, check the application server documentation for settings that might affect the classloader behavior.


              • #8
                thanks a lot for the help. those responses worked.