karldmoore development

A development oriented blog, containing anything I've been working on, reading or thinking about.

  • Subscribe

  • Twitter Updates

    Error: Twitter did not respond. Please wait a few minutes and refresh this page.

  • Disclaimer

    The opinions expressed here are my own and are not necessarily shared by my employer, any other organization, or any other individual. Any trademarked names or labels used in this blog remain the property of their respective trademark owners. No guarantees are made regarding the accuracy or usefulness of content on this blog, though every effort is made to be accurate.
  • Meta

Posts Tagged ‘Spring’

Spring Recipes: A Problem-Solution Approach

Posted by karldmoore on February 17, 2009

springrecipesI recently had the pleasure of reading Spring Recipes: A Problem-Solution Approach. I first began reading the sample chapters released on the authors website, so when the book was finally finished I was really looking forward to getting hold of a copy. When it arrived I was amazed to see that it weighed in at a hefty 752 pages, but that really shouldn’t put you off.

Spring Recipes covers Spring 2.5 (and earlier versions) taking it from the simplest concepts right through to the most advanced level. The book is divided into nineteen chapters organised into three parts; Core, Fundamentals and Advanced. Each part builds on the previous ones to grow your understanding of all aspects of the Spring framework, for this reason the book is best read cover to cover.

This book is in no way filler and is packed full of content covering not only the Spring framework and but also several common Spring projects including; Spring Security, Spring Web Flow and Spring Web Services. For anyone that knows Spring (and it’s associated projects), this is a huge amount of information to cover in just one book, but this book really does deliver!

As the author puts it; “Reading Spring Recipes will help you master the most important 80 percent of Spring’s features with just 20 percent of the effort you would need to learn them all.” If you are already using Spring, this book will serve as an invaluable reference and companion guide for future development. If you are a newcomer to Spring, this book will serve as an excellent launch pad to get you up, running and productive with Spring very quickly.


Each of the recipes covered in the book follows; a problem, solution and then how it works approach. This approach lays out many of the common FAQ style questions that come up on Spring Forum in an accessible way; clearly outlining the problem and mapping the path to the solution. This book also acts an excellent companion to the Spring Reference manual, which is sometimes difficult to read in places and lacks the array of examples this book provides.


The intended audience for Spring Recipes, is any Java programmer hoping to learn more about Spring. No prior knowledge of Spring is required as this book covers it all right from core concepts such as inversion of control (or Ioc). This book is accessible to anyone who has heard the hype about Spring and simply wants to learn more about it and how they can leverage it’s power. This book gives Java developers hands-on experience using the Spring framework with real-world examples, this experience can be transferred straight into your Spring project.


Part 1: Core

This part focuses on the core concepts and mechanisms of the Spring framework. The chapters in this part aim to familiarize you with Spring’s core. As a newcomer to Spring Chapter 1 introduces the concept of inversion of control the core foundation of Spring. It explains how and IoC container works, but also why it is useful and beneficial to use one. This chapter serves as an excellent introduction to IoC and is invaluable to anyone who isn’t quite sure why they should use a container based approach. This chapter is a must for anyone new to Spring or anyone trying to deliver it to a new audience.

Chapters 2-4 build on the IoC introduction, explaining how to setup and configure a new Spring project and apply the basics of Spring configuration. These first four chapters are the foundation for the rest of the book and are a must read for anyone who has never used Spring before and even advanced users may find useful hints and tips within. Chapters 5-6 cover the extremely powerful approach of AOP, introducing crosscutting concerns and how these can be configured with Spring. This technique can be extremely useful to provide trace logging, caching and other crosscutting concerns such as security. This is a must read for anyone using Spring, even if you are not directly leveraging Spring AOP or AspectJ. Spring AOP lays the foundation for introducing how Spring provides features such as transparent transaction support and method level authorization checking.

Part 2 : Fundamentals

This part has a split focus, it introduces Spring’s Data Access support, Web integration and also Testing support. Chapter 7 introduces one of the most important aspects of Spring’s data access support in the form of JdbcTemplate. This shows the reader how they can use JdbcTemplate to prevent the use of direct JDBC and instead let Spring handle all of the boiler plate code. As JdbcTemplate removes the need for the common try/catch/finally/try/catch code seen with JDBC, the code is much cleaner and much more readable. Along with this support, the Spring DataAccessException hierarchy is introduced which provides a rich set of common exceptions to provide a more granular handling approach.

Chapter 8 introduces Spring’s excellent transaction management support and explains in great detail all of the various propagation behaviours and isolation levels. This chapter is one of my favourites as it not only introduces the concept of Spring transaction support but transactions in general. It’s accessible to everyone and extremely useful when mentoring new developers.

Chapter 9 discusses how to integrate Spring with existing ORM frameworks, making their usage within the application seamless. This provides multiple approaches to integration allowing you to build on existing Spring classes, or completely decouple your classes from Spring removing the need for any Spring imports within your code. Chapters 10-11 discuss Spring’s popular MVC framework and also discuss how many other leading web frameworks can be integrated with a Spring application. As many of the projects I have worked on in the past have used Struts, Chapter 11 was particularly useful to outline all of the options available to use Spring and Struts together.

This part finishes with the introduction of Spring Testing Support, teaching you not only what features Spring has to offer but also how it integrates with popular testing frameworks such as JUnit and TestNG. As the Spring Testing support has been overhauled, it was great that this book covered the TestContextframework, but also legacy test classes to cover older versions of JUnit and TestNG. This chapter helps you leverage not only IoC for your tests but also transaction support, mock testing and also MVC integration testing. This approach enables you to easily decouple your code from your tests, providing first class support for all your testing needs.

Part 3 : Advanced

For anyone that has mastered the first twelve chapters, Chapter 13 is where things start to get really interesting. This chapter is dedicated to one of the most scary Spring projects, namely Spring Security (formerly known as Acegi). Spring Security provides a full range of authentication, authorization and access control options which allow it to secure a standalone Java application or a web-based application. This book focuses on version 2.x onwards which dramatically reduced the size of the required configuration and also its complexity. Spring Security can be a difficult subject to tackle as it’s often hard for developers to understand it’s concepts, but this chapter builds security into the example application in piecemeal steps. This demystifies many of the problematic areas of Spring Security whilst addressing the common problems a new developer might face.

Chapter 14 covers the portlet application development using the Spring Portlet MVC framework, highlighting the portlet-specific features which differ from the standard Spring MVC framework. If you don’t require the portlet features currently within your application, this chapter can safely be skipped as the next one is another gem. Chapter 15 covers the Spring Web Flow framework which addresses the three major pain–points facing web application developers: user interface navigation control, state management, and modularity. Spring Web Flow offers a flow definition language which encapsulates a reusable sequence of steps that can execute in different contexts. This framework is extremely flexible and powerful, but this chapter provides a great introduction into what is a very large subject.

Chapter 16 introduces the remoting integration that Spring provides for various technologies such as RMI, Hessian, Burlap, HTTP Invoker, and Web Services. Having already been familiar with many of these technologies, this chapter provides an excellent insight into how these can be integrated with Spring and how quickly this can be achieved. This chapter concludes with a look at Spring Web Services including how to develop contract-first web services. Chapter 17 discusses how to develop EJB 2.x and 3.0 components with Spring’s EJB support, and how to use Spring’s JMS support to simplify sending, receiving, and listening to JMS messages. This introduces a powerful concept of message-driven POJOsfor those who like the concept of message-driven beans (MDBs) but don’t want the restrictions of an EJB container.

Chapter 18 demonstrates how easily Spring beans can be exported as JMX MBeans with relative ease in comparison to the typical approach. It continues to extend this by providing remote access, auto-detection and notification. This chapter continues by introducing Spring’s email support which removes the reliance on JavaMail specific mail sessions and exceptions. This provides an abstraction which is independent making it easy to switch to another e-mail API. This chapter finishes with a look at the options available for scheduling tasks within the Spring framework, this integrates with the JDK Timer and also Quartz scheduler.

Chapter 19, this part and the book concludes with a look at the various scripting languages that can used with Spring applications including; JRuby, Groovy and BeanShell. One of the most powerful features of using scripts is that any changes made to these can be detected by Spring and thus the beans can be refreshed automatically. This is of great benefit when you wish to avoid the long compile, deploy and test cycles or the application cannot be taken down regularly for maintenance. This chapter demonstrates how scripted beans can be added to your application quickly and simply using Spring.


This book has undertaken a mammoth task, trying to squeeze Spring and associated projects all into one book. This book is very clearly written, with well-structured, concise examples that get to the heart of the problem without complicating it with unnecessary detail. The examples provide solutions to real world problems instead of taking the form of the simplistic examples found in many other technology books. My only real minor criticism of this book is it’s scope and size. Given the huge undertaking, there are obviously going to be some areas which only provide a minimal amount of information. This should be expected given the vast array of information covered, but there are a range of Apress books which can build on this foundation. I would still give this book five out of five and rate it as one of the best framework specific books I’ve read.

This book should accompany anyone starting their journey with the Spring framework and even seasoned developers. It can take you from knowing nothing about the Spring framework to knowing pretty much everything you need to create a fully functional web application with all the bells and whistles you’d expect. It provides information and examples on numerous integration points with other popular open source frameworks and languages allowing you to truly become a Spring expert. Read it, read it again and keep it on your bookshelf as a solid reference for the Spring framework.


Posted in Books, Development, Spring | Tagged: , , , , , , | 2 Comments »

SQL n + 1 Selects Explained

Posted by karldmoore on February 5, 2009

The SQL n + 1 selects problem is extremely common but I have often found that many people have either never heard of it or simply don’t understand it. It is actually very easy to introduce a problem like this into your code, but it’s also very easy to resolve as well. Problems like this are best explained with an example; so imagine we have a table called users and another called user_roles. These tables are setup with a one-to-many relationship, meaning that one user (e.g. jsmith) can have many roles (e.g. Administrator, Auditor, Developer). Many people might implement something like this;

public Iterable<User> allUsers() {
    final String selectUsers = "select users.username, users.email, " +
        "users.last_password_change from users";
    return getJdbcTemplate().query(selectUsers, new Object[] {},
                new ParameterizedRowMapper<User>() {
        public User mapRow(ResultSet resultSet, int rowNumber) throws SQLException {
            String username = resultSet.getString("username");
            String email = resultSet.getString("email");
            Date lastPasswordChange = resultSet.getDate("last_password_change");
            User user = new DefaultUser(username, email, lastPasswordChange);
            return user;

private void addRolesToUser(final User user) {
    final String selectUserRoles = "select role_name from user_roles where username = ?";
    getJdbcTemplate().query(selectUserRoles, new Object[] { user.getPrincipalName() },
                new RowCallbackHandler() {
        public void processRow(ResultSet resultSet) throws SQLException {
            String rolename = resultSet.getString("role_name");

Reviewing the code we can see one query is executed to retrieve the users, the problem here is for each user another SQL statement needs to be executed to retrieve the roles. If the first query retrieved one user, this would require one additional query to retrieve the roles. If the first query retrieved a hundred users, this would require one hundred additional queries to retrieve the roles. The pattern will always be the same, one query for the users and n queries dependent on the number of users found, thus n + 1. Although this solution is functional, it does result in many unnecessary SQL statements being executed.

select users.username, users.email, users.last_password_change from users;
select role_name from user_roles where username = ?;
select role_name from user_roles where username = ?;
select role_name from user_roles where username = ?; 

Shared resources are typically the bottleneck in most applications, so expensive or unnecessary SQL should be avoided if possible. As the application attempts to scale, this bottleneck can become extremely problematic and severely inhibit application performance. Fortunately this is a simple solutions to this problem; introducing a join into the query.

public Iterable<User> allUsers() {
    final String selectUsers =
        "select users.username, users.email, users.last_password_change, user_roles.role_name "
            + "from users left join user_roles on (users.username = user_roles.username)";
    final Map<String, User> users = new HashMap<String, User>();
    getJdbcTemplate().query(selectUsers, new Object[] {}, new RowCallbackHandler() {
        public void processRow(ResultSet resultSet) throws SQLException {
            String username = resultSet.getString("username");
            if (!users.containsKey(username)) {
                String email = resultSet.getString("email");
                Date lastPasswordChange = resultSet.getDate("last_password_change");
                User user = new DefaultUser(username, email, lastPasswordChange);
                users.put(username, user);

            String rolename = resultSet.getString("role_name");
            if (!StringUtil.isNull(rolename)) {
                User user = users.get(username);
    return users.values();

Although the code and SQL statement are slightly more complex that the original example, it results in much fewer SQL statements being executed. Instead of the n + 1 statements executed in the first example, one statement is executed that fetches all the required data. This typically results in much improved performance and as the numbers scale the improvement in performance can become much more apparent.

select users.username, users.email, users.last_password_change, user_roles.role_name
    from users left join user_roles on (users.username = user_roles.username);

As with all performance optimizations the most important thing is to measure the effect of the improvement. Performance optimizations aren’t always predictable so by taking measurements before and after the change, you can accurately know if you have actually improved the performance (or made it worse). A SQL join may be the most appropriate way of solving a problem such as this, but there are other alternatives such as caching the data instead. Although the SQL n + 1 selects is an extremely common problem, is not always well understood and is often still found within code. It is very easy to introduce a problem like this into your code, but it’s also very easy to resolve as well. Next time you are viewing your debug output, see if you can spot SQL n + 1 selects.


Database access using Spring JdbcTemplate
Preventing the n + 1 select problem when using Hibernate

Posted in Development, Refactoring, Spring, SQL | Tagged: , , , , , , , , , | 5 Comments »

Parleys.com: My New Favourite Site

Posted by karldmoore on October 18, 2008

parleys-logoFor the past few months, I’ve been spending most of my lunch breaks viewing the plethora of content available on Parleys. I first heard about Parleys courtesy of Marten Deinum, during a conversation on Spring Forum. Back then it seemed to be a well kept secret, but according to it’s website, 45,000+ people have now visited the site.

For those of you who haven’t had the pleasure of visiting Parleys and don’t know what it is, the home page describes it as:

Parleys.com is a new BeJUG initiative where the different recorded talks from JavaPolis, BeJUG, SpringOne, JaZoon, JavaZone and EclipseCon (and hopefully others in the future) get published on a regular basis.

The Parleys.com site wants to become the premier Java e-learning site where you can listen and subscribe to many Java related podcasts or view the flash talks, hopefully all resulting in improving your Java skills.


There’s a wide array of presentations available (over 100 at last count), so there should be something there to interest you. Each presentation gives you a handy overview of the content, the running time and also the votes that have been cast. All of these together, make it very easy to choose something to fill your lunch break.


The presentations provide audio, video and also slides to accompany it. It’s great to be able to leave the presentation going in the background whilst I’m do something else. If I really have nothing to do, it’s great to be able to view the additional content the slides provide. One feature I’ve found invaluable however, is the ability to skip through the agenda. When you have to get back to work, it’s so nice to be able to carry on watching from where you left off when you get home.


If you haven’t already used Parleys, I would thoroughly recommend a visit. I’ve found Parleys to been an invaluable tool for learning about new technologies and techniques. Over the past few months, I’ve seen a large number of presentations on topics that I previously knew virtually nothing about. There are typically new presentations added at least every couple of weeks, so I’m looking forward to seeing what content is added next. And lastly don’t forget to vote for the presentations, it’s great to know what other people did or didn’t enjoy!

Posted in Opinion | Tagged: , , , , , | 11 Comments »

Are Conferences Really Worth The Money?

Posted by karldmoore on September 10, 2008

Nearly a year ago, I posted a question on Spring Forum entitled, “am I working for the wrong companies or is it just not that common to attend conferences?”. I’ve worked as a developer for over six years and as yet I’ve never had the opportunity to attend a single conference. I thought I might be alone in this, but I’ve spoken to a large number of developers who are in exactly the same position, some of whom have worked in the industry far longer than I have.

Having worked for small companies in the past, I can fully understand the problems associated with sending developers to conferences. Sending one developer doesn’t cost a vast amount of money, but if all your developers want to go the money really does start to mount up. After the issue of cost, you then have to factor in the lost development time, not only for the conference but travelling to and from the event as well. The various costs to the business can soon become very prohibitive.

Back in January of this year, I decided that I would make sure I attended a conference this year, and decided I would just pay for it myself. I wanted to see if conferences really are worth the money. After months of discussing it on Spring Forums, I had the pleasure of attending SpringOne in June. The conference, hotel, travel and other costs came in at around £800 (€1200 or US$1600) in total. I also decided to take a weeks holiday from work to make the most of my trip.
Read the rest of this entry »

Posted in Opinion, Spring | Tagged: , , | 6 Comments »

Spring Framework: Where Can I Find Examples And Resources?

Posted by karldmoore on September 3, 2008

When ever I’m working with Spring, one of the most common questions I get asked is “where can I find good quality examples and resources?” I hate to disappoint people that I have some secret stash of information, but I get everything I need from the same places as everyone else! Here’s a brief overview of the places I visit:

  1. Spring Framework reference documentation – I’ve found the reference manual to be the best source of information on the framework. Anytime I have a question, the first place I look is here! It’s constantly updated, and it contains lots of recommendations and best practices from the Spring development team. What better recommendations can you have then the people that wrote it!
  2. Spring Framework support forum – If you’re still left with questions after looking through the reference documentation, the Spring support forum provides a very active and friendly community. There are thousands of answers already on there, so it really pays to do a quick search before you post. If you do post a question make sure you do it the smart way.
  3. Spring Source Team Blog – Spring from the source. What better place to get information and updates on what the Spring development team are doing.
  4. Spring Framework Javadoc – If you want to find details on the API of the Spring classes, the comprehensive JavaDoc is the place to go.
  5. Spring Hub – An independent site containing lots of links and resources related to Spring.
  6. Spring Framework JIRA – If you think you’ve found a bug or want to propose an enhancement, JIRA is the place to go.
  7. Spring Framework Mailing List – If you want to follow the progress of Spring development, you can view the mailing list here or even subscribe.
  8. Sample Applications – Spring ships with a number of excellent sample applications. Look through them, play with them and understand them. Many people use these examples as a base for their applications, they are an excellent starting point.
  9. Source code – One of the most over looked resources that Spring has to offer is it’s source code. It’s well structured, well written and easy to follow. It’s also a great place to get answers. The source code for the unit tests is also included with the release. What more information could you be looking for?
  10. Spring Books – If you are still looking for more information, there are a wide selection of books available. There are several existing threads on Spring forum that discuss the various books and their relative merits.

Posted in Spring | Tagged: , , | 2 Comments »