Demystifying Spring: Let’s Build a Simple Framework to Understand Spring Internals
Most people do not understand how dependency injection frameworks like Spring, work underneath. I have seen people wondering, why @Transactional annotation does not work in private methods and when called in the same bean. So people see it as a magic. But in reality, it is just some set of basic programming concepts put together.
Don’t be a frameworker. Learn the concepts!
The best way to learn the internals is to build a simple framework from scratch. In this tutorial series, I am going to show you how basic annotations like @Transactional and @Cacheable work behind the scenes.
You can see the source code in my GitHub Repository: https://github.com/diliproshitha/simple-framework
Let’s start..
Here is the project structure I am going to follow:
I am creating a new package for each new part of this tutorial series. So you can go through each part and easily identify the differences. Inside each package, I will be creating two sub packages for application and for the framework.
Under part 1, I am creating basic model, Dao (Data access object), service layer and main class.
Model: Customer.java
Data access objects: CustomerDao.java and CustomerDaoImpl.java
Here we are just logging some text into console when save and delete methods are called.
Service: CustomerService.java and CustomerServiceImpl.java
Before each operation, we are starting a new transaction by calling beginTransaction() method. If the save or delete operation is successful, then we call commitTransaction() method to commit the changes into Database. If something went wrong, then we call rollbackTransacion() method to rollback the changes.
In here we are not going to implement actual database operations, we are just logging some text into the console.
Main class: Part1Application.java
After running above class you will see an output similar to this:
To make this application looks like Spring, we need to add proxies into the application. We will discuss about it in the part 2 of the series.