aspose file tools*
The moose likes Spring and the fly likes XML: multiple bean references - how to change them in one line? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "XML: multiple bean references - how to change them in one line?" Watch "XML: multiple bean references - how to change them in one line?" New topic
Author

XML: multiple bean references - how to change them in one line?

Paul Piesiak
Greenhorn

Joined: Sep 06, 2011
Posts: 6

Hello.
I'm quite new to Spring.
While working on my application I found a part of the xml file that I think can be done better.
I'm using two xml files:
the first one (context.xml):


and the second (widokiContext.xml):


As you can see the beans "liniaWidok", "polaczenieWidok" and "przystanekWidok" have a reference to a bean "fileDataReader". You can also put a "dbDataReader" bean as a reference there. The problem is that always, when I need to change the reference in all 3 beans, I have to make a change in 3 places.
I'm guessing that there is a better way to achieve that, probably making a change only in a single line, but I can't figure out how.
I would be very thankful if someone could help me.
(Oh, and sorry if my english isn't too good. I didn't have the opportunity to use it for a long time).
Paul Piesiak
Greenhorn

Joined: Sep 06, 2011
Posts: 6

Ok. It seems I found a solution: I added the bean "appController" and now this bean has a reference to "fileDataReader" or "dbDataReader". So the "liniaWidok", "przystanekWidok" and "polaczenieWidok" beans will have a reference to "appController" instead of "fileDataReader" or "dbDataReader". And when I need to change the dataReader I simply change it only in one place - the "appController".
Here are the files including this solution:

controllerContext.xml:


widokiContext.xml:


context.xml stays the same:


This problem look quite silly now when I managed to solve it
But if anyone has a different idea please, let me know.
Cheers.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

You can do bean definition inheritence. Like..



Now all three will be scope prototype and have its dataReader set to fileDataReader. If you want to change that to dbDataReader you can just change



to



or if you don't have bean definition inheritance another approach is to use the alias tag. But you will first change the three beans to ref="someOtherNameUsedAsAnAlias"

then

<alias from-id="fileDataReader" to-id="someOtherNameUsedAsAnAlias"/>

Now the attributes of the alias tag might be called something different, but I haven't used the alias tag myself, so I didn't memorize those names.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

In your solution you posted, you just added a class layer that probably doesn't need to be there. So another class to maintain. But as you have seen it still works.

Mark
Paul Piesiak
Greenhorn

Joined: Sep 06, 2011
Posts: 6

@Mark:
Your first solution was the exact thing I had in mind.
Thank You very much.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Paul Pumpkin wrote:@Mark:
Your first solution was the exact thing I had in mind.
Thank You very much.


That is what I thought?

Hahaha I like the twitter @Mark.

Also, please check your Private Messages for an important JavaRanch message.

Thanks

Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Mark Spritzler wrote:You can do bean definition inheritence. Like..


Ooops I did have one typo.




should be



forgot to add a closing qoute to "dataReader".

Mark
Paul Piesiak
Greenhorn

Joined: Sep 06, 2011
Posts: 6

Mark Spritzler wrote:
Paul Pumpkin wrote:@Mark:
Your first solution was the exact thing I had in mind.
Thank You very much.


That is what I thought?

I mean that the bean definition inheritance was the thing I was looking for as a solution. Just didn't know how to achieve that.

Didn't realise that the "@Mark" is something unusual At my university forum You can see it more often. Though it might seem unnecessary since there's only the two us writing in this thread

Mark Spritzler wrote:
Also, please check your Private Messages for an important JavaRanch message.

Checked it, sorry for that. Already taken care of.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Thanks Paul.

Ask as many questions as you want here. We enjoy helping people out.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XML: multiple bean references - how to change them in one line?