This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Spring and the fly likes ClassCast exception in lookup-method injection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "ClassCast exception in lookup-method injection " Watch "ClassCast exception in lookup-method injection " New topic
Author

ClassCast exception in lookup-method injection

Siddesh Basapura
Greenhorn

Joined: Nov 03, 2010
Posts: 4
Hi,

I am new to spring. I am working on bean instantiation using look-method injection. Here is my code

CommandManager.java


Command.java


MyClass.java


context.xml


Main.java




When i execute Main class i am getting following exception:

Nov 3, 2010 4:15:35 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@14ed9ff: display name [org.springframework.context.support.ClassPathXmlApplicationContext@14ed9ff]; startup date [Wed Nov 03 16:15:35 GMT+05:30 2010]; root of context hierarchy
Nov 3, 2010 4:15:35 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [context.xml]
Inside Default Constructor of CommandManager and instantiating bean
Inside setBeanFactory()
BeanFactory =org.springframework.beans.factory.support.DefaultListableBeanFactory@329f3d: defining beans [commandmanager,command,myclass]; root of factory hierarchy
Inside MyClass Default Constructor
Before Calling bean
After calling bean : com.spring.MyClass$$EnhancerByCGLIB$$9dea8484@a3d4cf
Before Calling createCommand()
Nov 3, 2010 4:15:35 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@14ed9ff]: org.springframework.beans.factory.support.DefaultListableBeanFactory@329f3d
Nov 3, 2010 4:15:35 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@329f3d: defining beans [commandmanager,command,myclass]; root of factory hierarchy
Exception in thread "main" java.lang.ClassCastException: com.spring.CommandManager
at com.spring.MyClass$$EnhancerByCGLIB$$9dea8484.createCommand(<generated>)
at com.spring.Main.main(Main.java:21)





Is i made any mistake in code or i missed out any thing......please help me.....
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
I'm rather new to Spring so I may be completely wrong. But nobody else is answering so I'll give it a shot.

According to your log, "Before Calling createCommand()" is printed but "After calling createCommand()" is not. That points at the statement between them, mc.createCommand().

I see that MyClass is an abstract class and that you define it as a bean. Can you do that? You also don't have a class that subclasses MyClass.

I see a log entry "After calling bean : com.spring.MyClass$$EnhancerByCGLIB$$9dea8484@a3d4cf" That looks suspicious. Why don't you log the class name before you call mc.createCommand(). That is, log mc.getClass().getName(). You can also add an if statement that checks the instance of the mc variable. Another thing you can do is place all the code in main in a try/catch block and print the stack trace on any exception.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

If you are new to Spring, I have some questions.

1) Why aren't you using the latest version Spring 3.x? You are using 2.5.
2) Still using 2.5, the "patterns" you are using are more Spring 1.x or 2.0 style then the best practices of 2.5 and higher. What materials are you using to learn? You might want to find something that is more up to date.

In current best practices, all your code should be POJOs and not extending or implementing any Spring specific classes or interfaces.

But you should always create interfaces for your classes. I noticed in your exception the part about CGLib, that means that it is creating a Proxy that extends your class, and not a DynamicProxy that implements your interface. Just a better practice, not completely wrong here.

And MyClass is abstract. Why? And do you have any implementation of this class anywhere. I think the error might be related, because whatever it has it is trying to cast it to either a Command or CommandManager, I can't quite tell in the exception.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Siddesh Basapura
Greenhorn

Joined: Nov 03, 2010
Posts: 4
Thanks guy's for your replay.
I am referring Spring Framework (2.0.2) reference manual to learn spring's. If this is older version where will i get the good manual with examples to learn Spring3.x?
Please suggest me the good book and also give me the link to refer examples.
And i am using SpringSourceTool.
Actually this is the example i get in the that book and also i found this example in this link "http://java-x.blogspot.com/2007/01/spring-prototype-beans-with-singletons.html" and i go through it.

As you told MyClass was abstract because in the manual they said "class containing the method to be injected must have the signature of this form<public|protected> [abstract] <return-type> theMethodName(no-arguments);" so i specified the class as abstract.

After changing MyClass as an interface and CommandManager is implementing this interface, still i am getting the same exception.

After some small changes in the context.xml file it's working fine. The changes i did is


In the lookup-method earlier it was pointing to look the bean CommandManager and i changed it to look command bean it is working fine and the below out put is comming


Nov 9, 2010 12:44:09 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@14ed9ff: display name [org.springframework.context.support.ClassPathXmlApplicationContext@14ed9ff]; startup date [Tue Nov 09 12:44:09 GMT+05:30 2010]; root of context hierarchy
Nov 9, 2010 12:44:09 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [context.xml]
Nov 9, 2010 12:44:09 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@14ed9ff]: org.springframework.beans.factory.support.DefaultListableBeanFactory@329f3d
Nov 9, 2010 12:44:09 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@329f3d: defining beans [commandmanager,command,myclass]; root of factory hierarchy
Inside Default Constructor of CommandManager and instantiating bean
Inside setBeanFactory()
BeanFactory =org.springframework.beans.factory.support.DefaultListableBeanFactory
Before Calling bean
After calling bean : com.spring.MyClass$$EnhancerByCGLIB$$44829686
Before Calling createCommand()
Default constructor of command creating command bean
After calling createCommand()



My question is lookup-method is defined inside the MyClass bean definition in the context.xml file. And when i am loading MyClass bean in main method, it should through exception.
(As per my understanding)Because when we load the bean, lookup-method is defined inside the bean definition and, the context loader will try to resolve the dependencies by instantiating
the bean by calling the createCommand method inside the command bean class. But there is no createCommand method inside the command bean class. Then How it is working without giving any exception? Is there any wrong in my understanding? If i am wrong please correct me.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

The documentation on www.springframework.org for Spring 3.0 should be what you are looking at. Spring 2.0 is very old and would not be beneficial in job searching as today people use minimum 2.5

There are plenty of books out there on 2.5 and a couple new ones here or about here that you can get. Check out and search this forum for book suggestions. There are lots of them.

Good Luck.

Mark
 
Consider Paul's rocket mass heater.
 
subject: ClassCast exception in lookup-method injection
 
Similar Threads
Problem running Spring Application
Spring AOP HELP needed
Some help required for understanding spring lifecycle
using ClassPathXmlApplicationContext to load beans
problem in my Spring Application program