Today I will write about a tutorial for spring and eclipse beginners. The original ‘Spring in Small Doses’ was posted here from Tiago Brito Spindola. Because I never really used eclipse I will try to describe additional steps, which will be necessary for eclipse newbies and I hope someone could benefit from it.I am a NetBeans user from 3.4 on, so I hope exploring eclipse features will be interesting and fun. For a nice comparison look here.
I chose the spring-tutorial, because the title suggested it could not get really difficult. As you will notice later the sources are well organized and you will learn concepts like ‘dependency injection’, too (if not already done).
Lets Start With The First Example
Download the four necessary things described in the tutorial:
Some questions related to eclipse arises in the following order, while I go through this example.
At the very first you have to define some user libraries.
How can you define user libraries with eclipse?
With Window->Preferences; Java->Build Path->User Libraries. (You could search within this window – nice!) you can do this.
Hint: I couldn’t find derby in the jdk path, but you can get the latest version here (I used 10.3.2.1). By the way: what is a workspace? Why you need it? If you have multiple projects opened? Or why?
Now it is time to compile the example files. To do so, you have to import the existing sources into eclipse.
How can you import existing projects?
- Window->Show View->Other; Search for ‘Project’->Select ‘Project Explorer’
- Right click into the window and click ‘import’
- Expand the ‘General’ node and select ‘Existing Projects into Workspace. Press ‘next’.
- Browse to the location of the root of example one and press ‘Finish’.
Now you are done with it and can close the project via right clicking on the project node and click ‘Close Project’. Go further with
The Second example
Import ‘Report Engine – Part 2′ like described above and right-click the root node of this project. Now go to ‘Run As …’->Java Application and choose ReportViewerStarter as main class. Or type Shift+Alt+x and then j – you will see a popup menu with all possible key combinations before you will type the ‘j’ – this is a nice eclipse feature to learn key bindings! Now you can type in countries and you will see some content in the table.
If you select SpringReportViewerStarter you will see the same frame – but it is different! Because now the sources doesn’t directly rely on the spring libraries!
Go on with
The Third Example
In this part the author of the original tutorials wants to use a database table instead of the file for the country data. You would say: ‘ups I will recreate the program from the scratch or what !? No, this is not necessary, because the author created the program with this ‘feature’ in mind and uses the spring libraries. But maybe he wouldn’t have this in mind – we always benefit from spring, because we decouple implementations from interface and the implementation even could change while running the program. So with spring libraries you are very flexible for later changes.
You will only need some changes to the configuration files and add a new jdbc factory class.
For this example you have to fill the database (derby) with some values. Derby is a really nice, small, powerful and secure database. I used it for my own projects.
How to set up derby?
Find the ‘derby.xml’ file in the ant folder. Then proceed as follows:
- Start the server with: Right click derby.xml and ‘Run As’->’Ant Build…’; Deselect ‘usage’ and select ‘start’. Then click ‘Run’. It is possible that you have to set the property derby.home to e.g. /home/peter/programs/derby. You can do this in this (ant-build) dialog: select the tab ‘Properties’ and deselect ‘Use global properties ..’. Then click ‘Add Property…’ and type in your necessary value for the folder.
- Fill the database via right-clicking on derby.xml and then click ‘Run As’->’Ant Build…’. Deselect the ‘start’ item and select ‘create’. Then click ‘Run’. You should get the following message: ’246 of 246 SQL statements executed successfully’ after some hard disc activity.
- Now run it as a Java-App: ‘Run As …’->Java Application and choose SpringReportViewerStarter as the main class.
Now, if you are finished, close the project and go on with
The Last Example
In this example you will again benefit from the ‘dependency injection’ pattern, because you will write a web GUI (via JSP) to query the database (-server).
Before building the war file you will have to set the properties spring.home and derby.home to an appropriated value. This was described in the previous example.
Now we can build the war file: Right click the build.xml file and choose ‘Run As’->’Ant Build’.
But how you can deploy a war file?Copy the war file to TOMCAT_HOME/webapps/. Go to the tomcat 5.5. folder where you decompressed it. Go deeper into the ‘bin’ folder and start tomcat:
JRE_HOME=/usr/java/latest && ./startup.sh
(In my case I had to change the default java home directory – thatwhy I added the JRE_HOME stuff.) Then go to: http://localhost:8080/ with your firefox browser and you will see the typical tomcat start page. If you seek for a better solution to handle deployment, this eclipse plugin could be useful for you. Another kind of deploying is to go to the ‘Tomcat Manager’ on the left of the side and then upload the war file. For this procedure you will need to set up a user with manager privileges. To do this go to the folder: conf/tomcat-users.xml and insert the following line, which ‘creates’ a user called ‘manager’ with manager privileges and mmaa123 as the password:
<user username=”manager” password=”mmaa123″ roles=”manager”/>
Now with the browser go to:
And you will see the list of countries as html in your browser.
The author even implements the Swing GUI for this example, i.e. the report engine will be running on the top of tomcat and the data will be transferred through a spring library. For this you should run the project as Java Application and choose SpringReportViewerStarter. If you don’t believe it, you can shut down tomcat via ./shutdown.sh. Then you will get a RemoteConnectFailureException if you click on the ‘Refresh’ button within the Swing GUI, which indicates that the program really needs tomcat
I hope I could show you how to get started with eclipse and furthermore the author of the ‘Spring in Small Doses’ could show you how to get started with Spring.
Finally I want to thanks Tiago Brito Spindola for his tutorial. For me his tutorial was very easy to follow, although I am an eclipse beginner.
Maybe other users know a better way of doing things in eclipse. Please let me know and feel free to leave your comments here.