Announcement Announcement Module
No announcement yet.
Best practise to acess bean in Legacy code Bundle Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Best practise to acess bean in Legacy code Bundle

    I have the following use case in my legacy application

    My bundle looks as follows with the following configuration

    <bean id="bean1" class="com.test.bean1" init-method="init"/>

    <bean id="bean2" class="com.test.bean2" init-method="init"/>

    Class Bean1
    public void init()
    Myclass m = new MyClass();


    Class Myclass()
    public void initilize()
    I WANT TO ACCESS OTHER BEANS(*** bean 2 ***) HERE ???


    One way to access is making my beans ApplicationContextAware and use this application context in my bundle to access the bean. But is that the best way or is there any other way out ?

    This boils down to me having one class implementing ApplicationContextAware
    per bundle :-( .

  • #2
    Why not inject bean2 into bean1's constructor?


    • #3
      Our legacy code is much more complicated that the simple example i have written below. It follows the inheritance model and in the init of bean1 ,we do our server initialization.

      This init calls 'n' numbers of classes , in between which few non spring container created classes will need spring managed beans.

      Passing these values through each class created during the process may be complex.


      • #4
        ApplicationContextAware isn't going to help you. Any bean that could be made application context aware could have the relevant beans injected directly into it.

        If your plan is to pass the application context around through the rest of your network of objects, I'd personally advise you to build your own facade and pass that around instead. Consider the Law of Demeter too. That will minimise the dependency of the bulk of code on the set of beans.