14.7K Followers
·
Follow

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.

Image for post
Image for post

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

Image for post
Image for post

3 Best Practices I learned from Spring Framework

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

Image for post
Image for post

An example of Coding for interface 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.

Image for post
Image for post

2. Favor Unchecked Exception over Checked Exception

Image for post
Image for post

3. Use of Template Design Pattern

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

Image for post
Image for post

Apart from 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.

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.

Written by

I am Java programmer, blogger, working on Java, J2EE, UNIX, FIX Protocol. I share Java tips on http://javarevisited.blogspot.com and http://java67.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store