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.
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.
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
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.