/ JDBC, SPRING

Age of Spring

Since it first version, Spring has known success. How is it possible that such an unknown framework (at the time) has become so widespread that companies demand to attendants to have Spring knowledge?

I think they are two main reasons for this. First, the use of Inversion of Control really helps unit testing your classes and since unit tests have become a hot topic, it is natural that a framework that promote independency between classes should win. But they are other IoC frameworks available. Did you hear about Guice? Yes, with a G like Google. Not really a small potatoes player, is it? Yet, it is a previously unkwown company like Interface21 (now renamed SpringSource) that made the reference product.

The second reason, and according to me, the most important, is that Java & JEE development is complex and thus costly. So, when a framework like Spring provides ways to speed up such development by giving integration components, it is natural it becomes the leader in its field.

For example, the following snippet shows how to execute a simple query on a database:

Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("jdbc/javadb");
Connection con = ds.getConnection();
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM CUSTOMERS WHERE NAME = ?");
pstmt.setString(1, "KERRY");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
    Customer customer = new Customer();
    customer.setName(rs.getString("NAME"));
    customer.setFirstName(rs.getString("FIRST_NAME"));
}

The same thing can be done in Spring like this:

RowMapper mapper = new RowMapper() {
    public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        Customer customer = new Customer();
        customer.setName(rs.getString("NAME"));
        customer.setFirstName(rs.getString("FIRST_NAME"));
        return customer ;
    }
};
jdbcTemplate.queryForObject("SELECT * FROM CUSTOMERS WHERE NAME = ?", mapper, new Object[] {"KERRY"});

Take a look at lines 8-10 of first snippet compared to lines 1-8 of second snippet: the mapping lines look the same (yet they are not since the Spring version is more object-oriented). On the other hand, the lines 1-11 of classic snippet are sumed up in only one line in the Spring snippet! Boilerplate code is provided by Spring and executed behind the scene.

In conclusion, I think that this integration state of mind is the biggest asset of Spring. It would be nice to see such approach in other API too.

Nicolas Fränkel

Nicolas Fränkel

Developer Advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). Usually working on Java/Java EE and Spring technologies, but with focused interests like Rich Internet Applications, Testing, CI/CD and DevOps. Also double as a trainer and triples as a book author.

Read More
Age of Spring
Share this