Announcement Announcement Module
Collapse
No announcement yet.
Accessing ApplicationContext from Bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accessing ApplicationContext from Bean

    Hi there,

    it might be a dump question, but what's the "correct" way to get the
    ApplicationContext from a Bean which I have got from Spring:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <bean id="toolBean" class="test.ToolBean" />
    <bean id="secondBean" class="test.SecondBean" />
    </beans>

    The aim is to work in the ToolBean with the SecondBean. The environment is a web-application, with an webservice.
    But I think it's not related to this environment.

    Thanks for your help

    Marc

  • #2
    Just inject the secondBean into your toolBean... No need to access the applicationContext.

    Just create a setter on your toolBean:

    Code:
    public class ToolBean {
    
        private SecondBean secondBean
        
        public void setSecondBean(SecondBean secondBean) {
            this.secondBean=secondBean;
        }
        
        public void doSomething() {
            this.secondBean.someMethod();
        }   
    }
    Add a little something to your config

    Code:
    <bean id="toolBean" class="test.ToolBean">
        <property name="secondBean"><ref local="secondBean"></property>
    </bean>
    And your all set.

    See the spring reference and examples for more info.

    Comment


    • #3
      Thanks, but I'd like to decide on runtime which bean to load. So do I have to inject all kinds of beans in the ToolBean? I thought that using getBean([selectedBeanAsString]) would be nice to use.


      Originally posted by mdeinum
      Just inject the secondBean into your toolBean... No need to access the applicationContext.

      Just create a setter on your toolBean:

      Code:
      public class ToolBean {
      
          private SecondBean secondBean
          
          public void setSecondBean(SecondBean secondBean) {
              this.secondBean=secondBean;
          }
          
          public void doSomething() {
              this.secondBean.someMethod();
          }   
      }
      Add a little something to your config

      Code:
      <bean id="toolBean" class="test.ToolBean">
          <property name="secondBean"><ref local="secondBean"></property>
      </bean>
      And your all set.

      See the spring reference and examples for more info.

      Comment


      • #4
        To access the applicationcontext make your bean applicationcontextaware (just implement this interface).

        However I think it is better to inject the needed beans, even with the getBeans you need to now somehow which bean you are going to need. So doing a getBean("beanname") or injecting them at startup would make much difference.

        I'm also a firm believer that we should stay away from the application context as much as possible and do as much as possible by DI.

        But if you would give some more information on what you are about to accomplish we might give you a better (more complete) answer.

        Comment


        • #5
          Originally posted by mdeinum
          To access the applicationcontext make your bean applicationcontextaware (just implement this interface).
          Thanks that seems to help me.
          Originally posted by mdeinum
          However I think it is better to inject the needed beans, even with the getBeans you need to now somehow which bean you are going to need. So doing a getBean("beanname") or injecting them at startup would make much difference.

          I'm also a firm believer that we should stay away from the application context as much as possible and do as much as possible by DI.

          But if you would give some more information on what you are about to accomplish we might give you a better (more complete) answer.
          It's easy to explain: I've got a bean, which is exposed as a webservice.
          This webservice should only have one method with the signature:
          Code:
          public Document processRequest(Document doc)
          The request is an xml document which should be processed on serverside. For this, I've included a node in the xml which defines which bean has to be executed. So the ToolBean has got the information which bean to get.
          Instead of using a large "switch ... case" or "if..else if...else" I would like to read this node and use the information to retrieve the related bean.

          Comment


          • #6
            ah that makes things more clear.

            We have a similair webservice however we use automatic marshalling/unmarshalling so our methods are defined like 'normal' business methods we would normally use in our applications.

            But your solution should work also. There are more solutions to the same/similair problem .

            Comment


            • #7
              Originally posted by mdeinum
              We have a similair webservice however we use automatic marshalling/unmarshalling so our methods are defined like 'normal' business methods we would normally use in our applications.
              Yes that was also my first idea, to use normal business methods, but I had to write a large amount of the (in my last mail) mentioned if..elseif...else conditions. And - of course - I'd like to be as flexible as possible..

              Comment


              • #8
                As alternative to implementing ApplicationContextAware/ BeanFactoryAware: Maybe you could have a FactoryBean that serves as proxy. It returns proxies implementing the common interface and internally retrieves the beans you need. To specify the String identifying the required bean, you could apply a static method feeding a ThreadLocal variable.

                Just an idea,
                Andreas

                Comment

                Working...
                X