Monday, August 20, 2012

A (very) brief foray into Spring 3.1


As mentioned in my last post, I'm reacquainting myself with Java web application development. This post covers my very brief foray into using Spring 3.1.

I created a basic starter app using a Maven archetype. The generated starter app included Spring MVC 3.1, Spring Security (didn't need this, but it was easy to control what URLs were protected or not), Tiles and JPA/Hibernate/HSQLDB for persistence.

In terms of content and code, there was an index page and its corresponding controller class. Most useful to me though was a User POJO (set up as an entity that worked with persistence) and the corresponding UserRepository and UserService classes. The basic pattern seems to be that controllers interact with services to get done what they need done. In turn, Services (may) talk to Repositories (Repository being Spring-speak for a good old fashioned DAO) to perform the usual create, read, update and delete operations.

The way this starter app was configured was primarily Java based (rather than the more traditional XML based configuration). This was interesting to see, although when googling around for examples one tends not to yet find so many using this approach.

OK, moving on. As mentioned in my original post, I am going to just put together a page that lists job data from a database, as well as providing a form to add more job data.

Building a page to display a table of information retrieved from a database

I needed to create several items in the project to achieve this:

  • A Job POJO –
  • A controller corresponding to that page –
  • A service for the controller to interact with –
  • A repository (i.e. DAO) to manage the persistence of jobs –
  • And of course a web page. Went with a regular JSP called jobs.jsp
The Job POJO had to be annotated appropriately with JPA annotations, thus:

After which it just worked. (Schema creation was covered by the setting in – good enough for this experiment).

The JobController had to be annotated as a controller and instructed what request to kick in for with the @RequestMapping annotation. Note also the @Autowired JobService – this is how we inject the JobService here:

Note the index method also has a @RequestMapping annotation. At this point I could have either just annotated the class or the method itself with both the value and method attributes like this:    @RequestMapping( value = "/jobs" method = RequestMethod.GET ) -- but instead they are separated in anticipation of a method that will handle the post from the form later on.

Another key thing here is to note our list of jobs is added to that Model. This is how it gets into scope for use on the view (JSP page) you’ll see below.

Moving on to the JobService, which shook out like this:

Note the @Service annotation, which I understand is just a specialized form of @Component, though obviously the intent is clearer. Its presence ensures the class gets picked up during initialization and can participate in dependency injection. (Additionally, @Controller and @Repository fulfill the same duty, ensuring classes marked as such are picked up during the class path scanning of initialization).

Just as the JobController used @Autowired to inject JobService, so JobService uses @Autowired to bring in JobRepository.

The @PostConstruct annotation is an EE annotation that is pretty neat. You can mark one method of a class with this and after dependency injection is complete it will be called, allowing constructor-like behavior. For here I am using it in a slightly wacky way, as a means to stuff in a few sample jobs to my database (only one example shown above for brevity).

Finally, the listJobs() method simply gets a list of Jobs from the repository (i.e. the DAO).

This brings us to the JobRepository which looks like this:

First off, note the @Repository class annotation which indicates that this is basically a DAO (as well as also ensuring it gets picked up and is able to be part of the whole DI setup) and @Transactional class annotation and able to participate in Spring transaction handling.

Next, the JPA EntityManager is defined with the @PersistenceContext annotation.

Then the two DAO methods we need follow; one for saving Jobs and one for getting a list of them all. The listJobs() method is annotated with @Transactional again, with the readOnly attribute set to true presumably ensuring this operation is inhibited from ever modifying data.

Finally there’s the JSP which looks like this (just showing the table used for presenting the list of jobs, obviously there’s a little more than this):

I don’t think there’s really anything to explain here assuming familiarity with JSTL

Adding a form to allow the user to add new jobs

Mostly as an excuse to write a little JavaScript I wanted the form for adding jobs to be hidden until a user chose to add one. At that point it should be revealed, they could fill it out, submit and the page would refresh with the new job added to the list and the form hidden again.

To achieve this I ended up adding:

  • A form with appropriate fields and a submit button
  • A small JavaScript function to toggle the visibility of the form
  • A model attribute to transport the form’s values over to my controller
  • An addJob method to handle the form post

Here’s two fragments from the JSP with the JavaScript and the form for adding jobs:

The JavaScript is completely unexciting as I just knocked out the first thing that came to mind.

More interesting is the use of the Spring form taglib (declared with <%@ taglib uri="" prefix="f"%>). The first key thing is the modelAttribute attribute of the <f:form> tag which marries up with the following in the JobController:

Through this we have an empty Job POJO ready and waiting to collect the values entered to the form.

The second key thing is that the path attributes of the <f:input> tags match up with the property accessors on the Job POJO.

Finally, here’s the method in the JobController to handle the form post:

This seems pretty straightforward. Note the @RequestMapping annotation indicating this should handle POSTs. The method simply takes the incoming Job and uses the JobService to persist it before redirecting to the jobs page (redirecting after a POST preventing subsequent resubmits of the form).


Initially I was a little frustrated with Spring since my starter app was clearly geared to be done in the latest and greatest style, with little to no configuration taking place in XML, whereas the vast majority of tutorials out there aren't trying to teach you that. Eventually, after finding enough resources to get me going though I really quite liked how it shook out. There's a lot of power in those annotations, leading to very little cruft, and pretty simple, readable code. Granted I suppose there's a lot of older applications still heavy with dense XML and other complications, but if it's headed away from that I like it quite a lot.


  1. I am working as fresher in Web Development Company.I have learn about many framework of Java like hibernate,JSF,Struct etc but I learn Spring from your site.

  2. Nên làm gì khi bà bầu hay bị chuột rútThời gian mang thai, một số bà bầu thường bị chuột rút, đây là tình trạng khiến nhiều chị em bởi cơn đau của chuột rút
    Bà bầu 5 tháng nên ăn rau quả gìDinh dưỡng cho chị em trong thời kì mang bầu là hết sức quan trọng, nó ảnh hưởng hoàn toàn đến sự phát triển của thai nhi
    Bà bầu 2 tháng nên uống sữa gì?Các bà bầu trước khi uống sữa cần lưu ý một điều quan trọng là phải đọc kỹ hướng dẫn sử dụng trên bao bì, hoặc có thể nhờ bác sĩ tư vấn để biết cách dùng sữa.
    Bà bầu 3 tháng đầu nên kiêng ăn những gì?Đối với bà bầu thì 3 tháng đầu tiên là 3 tháng vô cùng quan trọng, đây cũng là giai đoạn khó khăn của không ít chị em gặp phải.
    Bà bầu tháng thứ 8 nên tăng bao nhiêu cân? Trong thời gian mang thai bà bầu sẽ có nhiều thay đổi, đặc biệt về cân nặng. Thời gian này 9 tháng này bà bầu có thể tăng cân rất nhanh và mức độ tăng cân còn tùy thuộc vào chế độ chăm sóc của bà bầu.
    Bà bầu không nên ăn rau quả gì?Hoa quả, rau xanh là thực phẩm giàu vitamin, dưỡng chất và các khoáng chất đặc biệt tốt cho bà bầu.
    Bà bầu ăn bưởi có tốt không? Bưởi là loại trái cây có vị chua, rất ngon, chứa nhiều thành phần vitamin rất tốt cho sức khỏe
    Bà bầu nên ăn gì cho con thông minh?Trong suốt thời gian 9 tháng mang thai, đối với bà bầu chế độ ăn uống, sinh hoạt nghỉ ngơi vô cùng quan trọng, việc này không chỉ giúp bồi bổ sức khỏe mà còn giúp thai nhi phát triển khỏe mạnh, phát triển trí thông minh cho bé sau này
    Bà bầu có nên ăn sữa chua không? Sữa chua không chỉ là thức ăn ngon miệng, mà là thức ăn bổ dưỡng, rất tốt cho da dẻ chị em và đặc biệt có thể chữa trị bệnh tốt.
    Những món ăn tốt cho bà bầuPhần lớn bà bầu đều cần dung nạp thêm nhiều chất đạm, một số loại vitamin và khoáng chất nhất định như axit folic, sắt và đặc biệt nhiều canxi.
    Bà bầu nên ăn thực phẩm gì để con trắngĐể giúp chị em giải đáp vấn đề này. Cùng đón xem bài viết sau đây nào các bạn.