• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

What is an overriding bean declaration?

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

DISCLAIMER: I am totally new to Spring, so excuse my stupidity.

Alright here's the question,

While working on beans.xml configuration file, I learnt that the bean's name should be unique, although duplicate names are allowed for overriding bean declaration. What does it mean?
All that I tried out was,
1. Have multiple bean declarations in the same bean config xml. Of course, it complained about the bean name being already in use.
2. Had 2 bean config files, copied the exact valid bean info in both the config files. No problem here

So, I dont know what is a overriding bean declaration and how to bean config files live/work in a project (potentially with same bean names and ids)?

Thanks,
Suhas.
 
Ranch Hand
Posts: 85
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am prepping for scjp 6 and overriding and overloading classes has gotten me nuts so overriding beans is a welcome change

lets see, You have a class where you try to make a web service call or post a msg on JMS, now you define a bean called "remoteCall" in mainAppContext.xml. (this is production code, it will work fine on any E2E environment) the class definbed as bean "remoteCall" lets say WSAndJMSClient.java knows everything about RPC.

now comes the overriding, You want to integration test your application without connecting to the actual WS or JMS, so you define a bean by the name of "remoteCall" in testAppContext.xml with the class as TestProxyClient.java (now this class does not have any of the RPC stuff and simply returns a success response...).

when the context is loaded in integration test mode load mainAppContext.xml first >> then >> testAppContext.xml and you can test your application without WS or JMS, For production dont add the test*.* files in the jar\war\ear and you have actual connectivity without changing a single line of code...


 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vivek,

Thanks for responding.....I am afraid I got nothing from your explanation.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vivek, did you reply at the wrong thread ?
 
ranger
Posts: 17347
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
So in the creation of an ApplicationContext it reads in your configuration. When it reads it in, it creates a bunch of BeanDefinition objects and puts them into a Map. The key of the map is your bean name, and the value in the map is the BeanDefinition instance for that bean. If you have two beans with the same name, then tries to put both into the Map. Well the second one wins, because what happens when you try to put in a Entry into a Map with a key that already exists in the Map. Well it will replace the value object in that slot, with the new one that is being added.

Hope that helps clear things up for you.

Mark
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark. I think it pretty much clears it up. Also, I did some bean dissection of my own where I found exactly the behavior you are talking about.

I had the same bean copied in two separate config files. Then loaded both the config files in the ApplicationContext and got that bean. I was expecting some sort of ambiguity error, but instead got an output. The output used the bean definition from the file that was declared later in the array of config files in the ClassPathXmlApplicationContext constructor.

Can you/someone confirm whether my inference is correct?

@Mark - So declaring a bean with the same name AND/OR id in two config files and loading BOTH config files is what is overriding bean declaration. Is that right?

Thanks,
Suhas.
 
Mark Spritzler
ranger
Posts: 17347
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

Suhas Wadadekar wrote:Thanks Mark. I think it pretty much clears it up. Also, I did some bean dissection of my own where I found exactly the behavior you are talking about.

I had the same bean copied in two separate config files. Then loaded both the config files in the ApplicationContext and got that bean. I was expecting some sort of ambiguity error, but instead got an output. The output used the bean definition from the file that was declared later in the array of config files in the ClassPathXmlApplicationContext constructor.

Can you/someone confirm whether my inference is correct?

@Mark - So declaring a bean with the same name AND/OR id in two config files and loading BOTH config files is what is overriding bean declaration. Is that right?

Thanks,
Suhas.



You are correct, and Yes to the last question.

Mark
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark.
 
Always! Wait. Never. Shut up. Look at this tiny ad.
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic