This is going to be a bit of a rant so bear with me!
I have tried to learn parts of Spring a few times but every time I do I run into road blocks very early on with very simple things.
At the weekend I started looking at some Spring MVC example and google led me to this tutorial. I ran into issues here as this example ancient and trying to find the jars for 2.5 proved nearly impossible as many of wouldn't download from the Maven repo(emtpy .jar.lastUpdated jars was all I was getting for some jars). Spring doesn't seem to do backwards compatibility so using the newer jars didn't work either. After a few hours of jar searching I got a sample application running only to run into a ClassNotFoundException when using tiles on my jsp page.
Error creating bean with name 'org.springframework.web.servlet.view.tiles2.TilesConfigurer#0' defined in ServletContext resource [/WEB-INF/spitter-servlet.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/tiles/TilesApplicationContext
It turns out tiles 3 is not backwards compatible with tiles 2 but only parts of my app seemed to require tiles 3 so gave up on this as I had spent hours doing nothing but trying to solve nothing but compatibility issues.
So I borrowed Pro Spring 3 from work as I had the original and found it to be a good source and decided to start from the start as there is a lot of new stuff in Spring 2.5/3.
One of the first things it suggests is to use SpringSource Tool Suite and after a quick glance it seems like it would solve a lot of my set up my issues.
So I follow their advice and set up a new "Simple Spring Utility Project" and create the first class which is :
The problems begin on line 3 - "The import org.springframework.context.ApplicationContext cannot be resolved".
The jar containing this class, spring-context-3.0.6.RELEASE.jar, was added by STS and the import on the next line is also contained within the same jar and resolves just fine.!
Line 8 gives an exception - missing type BeansException. Again spring-beans-3.0.6.RELEASE.jar was added as a Maven Dependencies.
I have practically done nothing here and a "Hello World" application is failing.
So has Spring become a nightmare for people new to it? I had spent a few weeks working with Spring 6-7 years ago and had far less problems with it back then - having 1 jar simplified things a lot for a new user but I understand the reasons for the change.
Yes, Spring is like a Swiss Knife in the sense that it's a multi-function tool, except that just being a bunch of knives, it's more or less like a drill, a jackhammer, a chain saw, and a loofah rolled into one. WHen you first look at it, you go "Wha?? What do I do with this. How do I hold it" . But, when you get used to it, you wonder how did you even live your life without it. You can do everything without Spring. Spring just makes it easier. And if Spring doesn't do it, you can easily make it easier by extending Spring.
Learning it is difficult though. 5 years ago when I first encountered Dependency Injection, I made a post that went "DI! how does it work?" There was a book promo going on at the time, and we had a guest author (not of a Spring book) and he very politely told me to get a book. I feel your pain. I really do
I would recommend that
a) learn Spring core before you learn any of the Spring libraries. A lot of Spring's power and magic comes from DI, IoC and AOP. You need to understand that first. The magic has grown a lot in the past 6 years, and unless you don;t understand core well, it might all seem greek to you
b) Don't dive into learning a Spring library before you understand core. THe core makes a lot of things magic, and unless you don;t understand the magic, it confounds you
c) Don't expect to get coding by looking at tutorials. Tutorials help once you understand what's going on. The problem is once you run into problems you can;t fix them because you don;t understand what's going on
d) I completely recommend Spring In Action. It starts with Core. I haven't read Pro Spring, but from the TOC, it seems like a good book
Now, to your specific issue. How are you executing the main function? Is it through command line or through the IDE? It sounds to me like Spring is on compile time class path, but it's not on runtime classpath. Just a hunch. What does the book say about running the app? I think if the book is specific about how to run the sample, then you should follow it just so you can focus on learning Spring rather than struggling with classpath issues
Personally, I like to run sample code inside JUnit. Especially because Maven has good support for JUnit, and it handles setting up the runtime class path for you. I would suggest that you follow the book's instructions, but if the book is not specific, put the sample code in JUnit. Maven will make it easy for you.
posted 5 years ago
Hello Jayesh, thanks for your reply.
I have an understanding of DI, IoC and AOP I just need more hands on experience.
I created the project with Eclipse+STS and copied and pasted the simple class above. The dependencies were downloaded and added automatically. The code doesn't compile because it cannot find the class ApplicationContext class and I have no idea why. It finds ClassPathXmlApplicationContext which is in the same jar. Adding an older or newer version of the jar solves the problem. Adding this 3.0.6 jar to another project gives the same problem - cannot find the ApplicationContext(it is present inside in the jar).
Once this problem is solved another I just another 'type not found' issue org.springframework.core.NestedRuntimeException. Once again I have this jar, the class is inside in it in the correct package structure. This was all done for me so it's not a question of making a mistake.
Now finally I have my three lines of code compiling but I have used jars from 3 different version of spring, 3.0.5, 3.0.6 and 4.0.6, to achieve it and I still have no idea why it's proving to be such a painful experience - this REALLY should just work.