Recently, I realized that Spring 2.5 was really old already. I was even called out for it:
Terence why do you start with Spring 2.5 now when Spring 3 is already over a year old? – Sean Patrick Floyd Feb 24 at 10:50
The quote came from a comment in my question on StackOverflow.
Anyway, I decided to try and use Spring 3.0 because of that. One of the new things that I encountered was the way Spring 3.0 maps the request to controllers. The dispatcher servlet doesn’t handle the URL mapping to controllers now. Instead, you use annotations to handle it automatically.
UPDATE: Apparently, I was wrong about the annotations. Spring already has annotation support in 2.5. I just didn’t get a chance encounter it back then which is why I thought it was completely new.
So I tried making another simple web application to try it out. First of all, I made a bean class:
Notice the @Controller and @RequestMapping annotation. Putting the @Controller annotation in your class automatically makes it a controller class in Spring 3.0. Due to this, you don’t need to declare it in your dispatcher servlet anymore. I guess that’s pretty cool.
Oh wait, since I can’t declare the URL mapping of the controller in the dispatcher servlet, how do I inject my beans to the controller manually now? This is what my dispatcher servlet looks like right now:
Notice the part that I commented out. That’s what I would have done if I wasn’t using annotations. Unfortunately, Spring 3.0 frowns upon not using annotations.
Anyway, running the program as is would result in a null pointer exception since this method won’t be getting any bean from the dispatcher servlet:
If I use the auto-wiring feature of Spring 3.0, I guess my problem would be resolved by now, but this excerpt from the book that I’m reading is giving me second thoughts of using the auto-wiring feature:
Although the auto-wiring feature is very powerful, the cost is that it will reduce the readability of your bean configurations. Because auto-wiring is performed by Spring at runtime, you cannot derive how your beans are wired from the bean configuration file. In practice, we recommend applying autowiring only in applications whose component dependencies are not complicated.
This was taken from the book, Spring Recipes – A problem-solution approach, 2nd edition by Gary Mak, Josh Long, and Daniel Rubio.
Yes, I know. I can’t really call my application complicated yet. I guess I’m just too lazy or too stupid to fix this problem on my own right now. Either way, I lost the motivation to learn Spring as of the moment. I can’t find a good introductory resource to Spring 3.0 that’s why I’m having a hard time right now. I really hope I get accepted at the code camp that I applied for. They’re going to teach Spring there. That might be my chance to finally have a good start with Spring.