This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Paul Piesiak
Greenhorn
Posts: 6
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Mark:
Your first solution was the exact thing I had in mind.
Thank You very much.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul.

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

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic