3 Coding Best Practices I learned from Spring Framework in Java
Spring is much more than just another DI and IOC framework and you can learn how to write better code using Spring.
There is no doubt that the Spring Framework is one of the most popular Java frameworks and makes it really easy to create real-world, enterprise-grade Java applications easy by providing features like dependency injection and inversion of control.
But, to be honest, Spring is much more than just another DI and IOC framework. It goes one more level to simplify many Java APIs like JDBC, JMS, Java Mail, etc by providing a useful layer of abstraction.
It’s much easier to work with JDBC with Spring’s
JdbcTempalte and other utility classes. They remove most of the friction Java developer faces with respect to executing SQL statements and processing
ResultSet to get the Java object they want.
So, when you learn Spring framework, you not just learn how to use it but also learn some useful insight about how to write better code in Java and Object-Oriented programming in general.
In this article, I am going to share some of the best practices which I have come across while learning Spring, mainly by reading my favorite Spring in Action book by Craig Walls and my own experience with the Spring framework.
Especially, the book, Spring in Action has a huge impact on me because of Craig’s excellent writing style and the way he explains each and every concept in Spring. If you have not read it already, I strongly suggest you read that book, it’s totally worth your time and money.
Here is the link to buy this book — Spring in Action
3 Best Practices I learned from Spring Framework
In the past, I have shared best spring courses and best spring books and today, I am going to share the best practices I learned from using Spring framework for 10 years. These best practices will also help you to write better code.
Anyway, without wasting any more of your time, here are 3 best practices from Spring I have learned and suggest every Java programmer be aware of it and apply it whenever they write code in Java.
1. Coding for Interfaces
This is an old OOP guideline that I have first learned while reading Head First Design Pattern. The main purpose of this OOP Design principle is to reduce coupling between two classes and thus increase flexibility.
Spring follows this Object Oriented guideline rigorously and often exposes an interface to use key classes like
JdbcOperation interface is created to leverage
JdbcTemplate. This practice promotes loose coupling between different layers.
Another good example of this is a
Cache interface that is used to provide caching. All other cache implementations like
NoOpCache implement this interface.
If your code is dependent upon the Cache interface and not on any specific implementation you can switch cache provider without impacting other parts of your code. If you are interested in learning more about clean code then Pyramid of Refactoring (Java) — Clean Code Gradually By Wlodek Krakowski on Udemy is a great resource.
An example of Coding for interface in Java
Here is a simple code example of coding for the interface in Java using the Collection framework. If you look closely, in this example, I have used an
interface instead of implementations for declaring variables, arguments, and return types of methods in Java.
This kind of coding style is
flexible and easier to change in the future. If you want to learn more about coding for interface principle, I suggest you go through the Basics of Software Architecture & Design Patterns in Java, an excellent course on this topic.
2. Favor Unchecked Exception over Checked Exception
If you have used Spring framework then you would notice that Spring favors unchecked exception over checked exception and the best example of this is Spring JDBC.
Spring has a rich hierarchy of exceptions to describe different errors you can get while connecting and retrieving data from the database but the root of them is
DataAccessException which is unchecked.
Spring believes that most of the error steps from the reason which cannot be corrected in catch block hence it leaves the decision to catch the exception of developers instead of forcing them as Java does. The result is cleaner code with no empty catch block, and fewer try-catch blocks.
This is also one of the best practices while dealing with errors and Exceptions in Java. If you are interested in that topic then you may also check my post 10 Java Exception best practice for more advice and if you need a course then The Complete Java Masterclass by Tim Buchalaka and his team is a great course to learn Java.
3. Use of Template Design Pattern
Spring makes heavy use of the Template method design pattern to simplify things. A good example of this is JdbcTemplate which takes away a lot of pain while using JDBC API. You only need to define what requires and Spring takes care of the rest of the process.
If you don’t know Template design pattern defines a process or algorithm where you cannot change the process but at the same time you can customize steps based upon your needs.
For example, while dealing with JDBC, you can use JdbcTemplate to execute a query and get the object you want. You just need to provide SQL which is different in each case as well as mapping logic to map a row from table to an object.
Here is a nice diagram that explains the Template design pattern nicely. You can see that every person has some common task but they do different work and that’s nicely captured by the Template method. All they need to define is their work, which they co by defining the
work() abstract method.
If you want to learn more about the Template design pattern then I highly recommend you check out Design Pattern in Java course by Dmitri Nesteruk on Udemy. This is a great course to learn the modern implementation of design patterns in Java
JdbcTemplate, you will also find a lot of other examples of template method patterns throughout the Spring framework API like JmsTemplate and RestTemplate which allows you to consume REST API from Java application.
Btw, if you are interested in learning more about using Spring for developing RESTful web services then REST with Spring MasterClass by Eugen Paraschiv is also a nice course to start with.
REST With Spring: The Master Class
The reference for building a production grade REST API with Spring. The 14 modules cover building and securing the API…
That’s all about some Java best practices you can learn from the Spring framework. Spring is a great framework and their authors are experienced Java developers.
You can learn a lot by using Spring as well as looking at their code, the decisions they made, and how they design their APIs. Spring is open-source, which means you can download and view their source code as well.
I know, Spring is a collection of many such best practices and there is a lot to learn but I have found these three used everywhere in Spring and thus have a huge impact on the code quality of Spring framework.
Anyway, if you have come across any other best practices you have learned from Spring Framework, feel free to share with us.
Other Java and Spring Articles you may like
Spring Frameworks MasterClass by Ranga Karnan
How Spring MVC works internally in Java?
10 Things Java Developer should learn in 2018?
10 Frameworks Java and Web Developer Should learn
10 Essential Testing Tools for Java developers
10 Tips to become a better Java Programmer in 2020
7 Reasons to use Spring for REST API development in Java
10 Free Spring Framework Courses for Beginners
10 Advanced Spring Boot Courss for Java developers
Why Spring is best for REST Web services in Java
10 Free Spring Boot courses for Java developers
Top 5 Courses to learn Microservice and Spring Cloud
My favorite courses to learn Software Architect
Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note
P. S. — If you are new to Spring framework and looking for a comprehensive course to learn Spring Framework online then I highly recommend you to join Spring Framework 5: Beginner to Guru course by John Thomspon on Udemy. It’s the best course to learn spring online.