Announcement Announcement Module
Collapse
No announcement yet.
ApplicationContext and getBean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:
    Code:
    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:
    Code:
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    context.getBean("testbean");
    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

    Comment


    • #3
      I just saw something on BeanFactoryAware.

      would this help? how can I use it?

      Comment


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

        Comment


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

          Cheers

          G

          Comment


          • #6
            I tried using the locator.
            beanRefFactory.xml
            Code:
            <bean id="gateway"
                     class="org.springframework.context.support.ClassPathXmlApplicationContext">
                    <constructor-arg>
                        <value>applicationContext.xml</value>
                    </constructor-arg>
                </bean>
            in class A
            Code:
            ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("beanRefFactory.xml");
            the beans in the applicationContext.xml are not loaded until I do
            Code:
            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:
            Code:
            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?

            Comment


            • #7
              1. There is no point in doing
                Code:
                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. 4.7.2.2. 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.

              Comment


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

                Comment

                Working...
                X