Results 1 to 4 of 4

Thread: Best practise to acess bean in Legacy code Bundle

  1. #1
    Join Date
    Nov 2008
    Location
    London,UK
    Posts
    299

    Default 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();
    m.initialize();
    ....
    ...........
    }

    }


    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. #2
    Join Date
    Oct 2008
    Location
    Winchester, UK
    Posts
    535

    Default

    Why not inject bean2 into bean1's constructor?
    Glyn Normington
    SpringSource

  3. #3
    Join Date
    Nov 2008
    Location
    London,UK
    Posts
    299

    Default

    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. #4
    Join Date
    Oct 2008
    Location
    Winchester, UK
    Posts
    535

    Default

    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.
    Glyn Normington
    SpringSource

Posting Permissions

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