Friday, July 3, 2009

Spring In Action - KnightApp modification

I'm currently reading Spring In Action by Craig Walls and Ryan Breidenbach from Manning Publications. Let me first thank them for writing this book as it's a reference that I've used both professionally and personally. I like the Manning "* In Action" series of books as they tend to get to the point with doing things through examples. However, I believe that the examples could have been a little better worked out, which I will point out here in a minute. Like any good technical author, though, they did include their source code for each of their examples.

In Chapter 1 they begin walking you through core Spring via the characteristic Hello World app pointing out Dependency Injection, and it's benefits. Their second example walks through a Knight application, and is used to demonstrate both dependency injection and Aspect Oriented Programming (AOP). I'm looking forward to seeing how Spring facilitates AOP.

They describe AOP as a technique that "promotes separation of concerns within a software system." It looks like the intention is to remove from application code those items that all portions of a system need to concern themselves with, such as logging, transaction management, and security. All classes in a system shouldn't have to concern themselves with these items as part of their application/business logic as they are concerns for the entire system being developed. I'll probably have more to post on AOP once I become more familiar with it's cross cutting domain and how this paradigm addresses them. The intention of this particular post is to point out a change you will need to make to one of your classes in this example or else it will not work.

While walking through dependency injection Walls and Breidenbach write a driver class to kick of the application called KnightApp.java. Their main in KnightApp.java is coded as follows:

-- KnightApp --
(Click for source)

package com.springinaction.chapter01.knight;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class KnightApp {

public static void main(String[] args) throws Exception{
BeanFactory factory =
new XmlBeanFactory(new FileSystemResource("resources/knight.xml"));
Knight knight = (Knight) factory.getBean("knight");
knight.embarkOnQuest();
}

}

As you move from using the Knight application from looking at dependancy injection to looking at AOP, they don't mention that the driver application KnightApp.java needs to be updated to no longer load the spring config file using BeanFactory and XmlBeanFactory to using ApplicationContext and ClassPathXmlApplicationContext, as shown below.

-- KnightAppModified --
(Click for source)

package com.springinaction.chapter01.knight;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class KnightApp {
public static void main(String[] args) throws Exception {
ApplicationContext ctx =
new ClassPathXmlApplicationContext(
"com/springinaction/chapter01/knight/knight.xml");

Knight knight =
(Knight) ctx.getBean("knight");

knight.embarkOnQuest();
}
}

As I mentioned earlier, like any good technical authors they included the working source code for their examples, as this is how I discovered what needed to be changed. But it does tend to be tedious to track down. I'm not entirely sure of the need for the change yet, but intend on discovering it as I go through more of the book.

2 comments:

  1. Yep...you see, the problem here is that writing a book using MS-Word and then writing code using Eclipse, I had no choice but to cut-n-paste-then-tweak code examples. There was no good way to just let the manuscript slurp in the code that I knew to work. The problem with cut-n-paste is that it's a manual process...and like all manual processes, something's bound to go wrong.

    But I'm glad you like the book. If you like the 2nd edition, then you're going to really like what I'm working on now...let's just say that third time's a charm!

    ReplyDelete
  2. Hi Craig,

    Thanks for taking the time to reply! I can imagine this would be a painstakingly tedious process...Yuk. Can't wait to get my hands on a copy of the next version.

    Take Care,
    Cory

    ReplyDelete