Announcement Announcement Module
Collapse
No announcement yet.
Is there a way to get a reference to the BeanFactory Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is there a way to get a reference to the BeanFactory

    Hi,

    I asked a similar question in the remoting forum (http://forum.springframework.org/showthread.php?t=21712) but the important part I want to know fits better in here.

    Is there a way to get the reference of the beanfactory to my java code. Can I inject the beanfactory somehow? As u can read in the other thread I need access to the beanfactory inside my java.

    thx in advance

  • #2
    Once you got hold of bean factory you can cast it (assuming it is safe) to AutowireCapableBeanFactory and then call "autowireBeanProperties(yourBean, autowire.mode, dependencyCheck).

    If you are getting any beans from Spring, then you can make that Spring implement ApplicationContextAware which will then provide you with an instance of the ApplicationContext.

    To get hold of the ApplicationContext, or a content from a bean that isn't managed by Spring depends (I think) on your environment; for example, if you have a ServletContext you can call WebApplicationContextUtils.getWebApplicationContex t(servletContext) etc.

    A bit more info if you please

    Comment


    • #3
      Ok. A little more Information.
      Axis2 is running as webapp and servlet independend from my spring environment. I have java code wich should have a reference to some beans.

      The java code is not a servlet and is not defined as bean in the spring context. So i can not inject the beans directly as I would on a normal spring bean. In fact the java class instances implementing my web services are created by axis2.

      My idea was now to use a class with a static method which holds the reference to the beanfactory and returns it to the requesters.

      This way I could look up the beans in my java code.

      The ContextAware method is called by spring while creating of the bean instance? This should work for me. I can set the BeanFactory and request it from my other source code.

      Hm. I'm thinking of a problem if 2 spring applications are running in the same jvm. If i have a static handle to the beanfactory the second start up overwrites my handle. Is this right?
      Last edited by soawork; Jan 25th, 2006, 09:49 AM.

      Comment


      • #4
        Originally posted by soawork
        The ContextAware method is called by spring while creating of the bean instance? This should work for me. I can set the BeanFactory and request it from my other source code.

        Hm. I'm thinking of a problem if 2 spring applications are running in the same jvm. If i have a static handle to the beanfactory the second start up overwrites my handle. Is this right?
        The ApplicationContextAware interface is called by Spring for beans which Spring manages.

        So to be clear; you have a non-spring managed bean and it uses beans that are managed by Spring. The question is how does the non-spring managed bean get hold of the Spring factory to get the beans; right?

        Hmmm, I am not sure. How are you instantiating the Spring application context in the first place?

        The ideal solution is obviously to have Spring managing the retrieval of all the beans, and you will find a large number of FactoryBeans that handle a large number of third parties. If you don't find one, they are very easy to create.

        This is certainly the situation I would be taking; i.e. not "how do I get hold of Spring", but rather "how can Spring get hold of these other beans".

        I am still not clear how you are instantiating the Spring application context in the first place

        Comment


        • #5
          Yes, the question is how to get references to the beans into other not spring managed beans (in fact not really beans but java classes).

          Spring is started with tomcat. I use the spring web mvc to generate a web frontend and for middle tier (with hibernate).

          I would like to use axis2 (for several strange reasons *g*) to publish some functionality as web service. My Problem is, there is no integration available (plzzzzzz correct me if I'm wrong).

          I think my solution is kind of ugly. Maybe I should use Singleton for the Managers so I could use only one Manager for Spring and Axis.

          But thx anyway

          Comment


          • #6
            If you have access to a standard ServletContext, and the spring applicationContext has already been bootstrapped then you can use http://www.springframework.org/docs/...textUtils.html.

            Comment

            Working...
            X