aspose file tools*
The moose likes Spring and the fly likes Getting different output in a Spring project Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Getting different output in a Spring project" Watch "Getting different output in a Spring project" New topic
Author

Getting different output in a Spring project

pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hello,

Could you please tell me why I'm getting a different output in the below Spring 3.0 program :-











This is my xml configuration file spring-idol.xml



I'm getting the below output :-

Playing DYNAMITE ...
using ...com.springinaction.springidol.Player@171bbc9
IN class Player, Playing VIOLIN.....


I was expecting "using VIOLIN......" in the second line, but don't know why it's different.
I cleaned up the project and rebuilt but no use..

Please advise.


Thanks,
Pramod
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Because

System.out.println(getInstrument());

Is going to call toString on your Instrument, and toString default implementation in Object is to print the Object. Which in your instance of instrument at that moment is

com.springinaction.springidol.Player@171bbc9

Try overriding toString to print "violin"

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Ah ... !!!
Thanks Mark.

pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Try overriding toString to print "violin"


Hey Mark,

Since "VIOLIN.." is defined in the conf file and which might change in future, what can I write during the @Override of toString() ?
or can I avoid this @Override and still get 'VIOLIN' printed using Spring ?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Also, you didn't post all the code. How does "using…" printout. Where is that code?

The only other way besides overriding toString is to cast your Instrument to the actual type to get to the String instrument variable value. But that would remove you from coding to an interface which is a best practice to always code to an interface. So for me the only option you have and what should be done is overriding toString. Look at Object classes implementation of toString and you will see why you got your value.

Mark
pramod talekar
Ranch Hand

Joined: Apr 26, 2010
Posts: 316

Hey Mark,

I just added System.out.println("using " + getInstrument()); to the original System.out.println(getInstrument()); to make it more meaningful. Sorry for missing it out over here..

The only other way besides overriding toString is to cast your Instrument to the actual type to get to the String instrument variable value.


You mean to say just like the other variable song which I written?
I thought so and that's why made it bit different to see what would happen..
Besides, I also changed few things like adding setters/getter and and trying out different things in xml file , but couldn't overcome that problem.
Will try out the overriding method and let you know.

Thanks a lot.


Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

The main point I am saying here, at this point. Is that your issue is a plain Java issue, that you would have using these classes without Spring. Spring isn't causing the Instrument's toString method printing the object reference.

Mark
 
 
subject: Getting different output in a Spring project