Results 1 to 8 of 8

Thread: Accessing ApplicationContext from Bean

  1. #1
    Join Date
    Jul 2006
    Posts
    11

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

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

  3. #3
    Join Date
    Jul 2006
    Posts
    11

    Default

    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.


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

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

    Default

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

  5. #5
    Join Date
    Jul 2006
    Posts
    11

    Default

    Quote Originally Posted by mdeinum
    To access the applicationcontext make your bean applicationcontextaware (just implement this interface).
    Thanks that seems to help me.
    Quote 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.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

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

  7. #7
    Join Date
    Jul 2006
    Posts
    11

    Default

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

  8. #8
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    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

Posting Permissions

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