Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

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

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

Mark
 
It's exactly the same and completely different as this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic