Spring MVC

Hi – My name is Matt Moran. I’ve been working for a couple of years at a company in my hometown of Birmingham, as a back end Java developer. One of the core technologies we use is Spring. Spring simplifies a lot of things – it enables you to do dependency injection (DI) also known as Inversion of Control (IoC). This lets you design classes independently, loosely coupled, with the dependencies assigned at run time. Spring has APIs that allow you to do your data access layer easily, and to create an application using the Model/View/Controller pattern.

I’m aiming, over the next few weeks, to demonstrate these principles by developing a simple web app that will create, read, update and destroy records on a MongoDB database using simple web forms set up with Thymeleaf and HTML. It will be called Matt’s Library Database System, since when I first got into IT many, many years ago, one of my first jobs was to design a library catalogue system.

Let’s sketch out the data model for this. Initially, let’s just store records for books. Let’s call the record (or document as MongoDB calls it) Book.

Example book:

{
   "book":{
           "title": "The Joy Of Clojure",
           "authors": {
                       "Michael Fogus",
                       "Chris Houser"
                      },
           "publisher": "Manning",
           "isbn": "1-6177291=41-2"
          }
}

…is how I would put it in my rough, hand-written JSON.

At this point it occurs to me that this might work better in Groovy than Java. Groovy’s a JVM language – it runs in the Java Virtual Machine – and it compiles to Java bytecode, so it’s compatible with pretty much everything else that runs in the JVM. The main differences with Java is that Groovy does a lot of stuff automatically, behind the scenes, and I kinda like that – it means a LOT less typing!

For instance:

Our book, in Java, would be represented as:

class Book {
   private String title;
   private String[] authors;
   private String publisher;
   private String isbn;

   public String getTitle(){
      return title;
   }
   public String setTitle(String title){
      this.title = title;
   }
   // further getters & setters here
   // for every. single. field!
}

…whereas in Groovy, getters & setters are implied, giving us a 6 line class. So much less hassle!

But Java is what I do, so initially at least let’s do this in Java, and then I’d like to refactor to Groovy once we’re done.

Leave a comment