THE ABSTRACTION OF APPLICATION COMPONENTS

spring

Abstraction is a technique based on Java’s interfaces that ensures the isolation of classes from their implementation. It will be clearer when we go to implement our test design. You will see a more complex project structure than you have seen until now, normally this is the structure of Spring-based projects.

CREATION OF THE CONFIG PACKAGE

We create the config package with the ConfigApp class inside.

ConfigApp

We’ve already seen the @ComponentScan notation I’ll tell you again that Spring will go looking in the notifications, service, repository packages for objects to be placed in the Context and marked with the @Component notation. We now create a new package that we will call model, with a Clienti class, normally in the model the entity classes that will be saved in a database should be specified. We are still not working with databases, so there will be some abstraction. I bring back the code for the Clienti class.

Abbiamo già visto la notazione ComponentScan ti ripeto che Spring andrà a cercare nei package notifications, service, repository gli oggetti da inserire nel Context e contraddistinti con la notazione @Component. Creiamo adesso un nuovo package che chiameremo model, con una classe Clienti, normalmente nel model vanno specificate le classe di entità che saranno salvate in un database. Ancora non lavoriamo con le basi dati, per cui ci sarà una certa astrazione. Ti riporto il codice della classe clienti.

THE PACKAGE REPOSITORY

Now we create a fundamental package that we will call repository. This package contains the interfaces and implementation classes that will allow us to interface with the databases. At this level it is still a simulation, so we will pretend that customer data are entered into the database with print lines. We create a new interface CustomersRepository with inside the method that saves the Clienti of our model in the repository.

THE IMPLEMENTATION CLASS

Keep in mind that all classes that will implement this Interface must mandatorily provide the implementation of saveClienti(Clienti cliente) however such implementations may differ from each other, that’s the big advantage of interfaces, there may be an implementation for SQL Server and, for example, one for MySQL. Now we are going to create always within the same package the first implementation class.

ClientiRepositori
ClientRepositoryImpl

I used the @Repository notation instead of @Component because it is essential to clarify what the role of this class is. Change the name but the functionality in Spring is the same, that class will be found thanks to ComponentScan and placed in the Context.

CREATION OF THE NOTIFICATIONS PACKAGE

We create a new notifications package, which will send the Client a notification. As we shall see we would have the option of specifying a notification by e-mail or SMS. Let’s look at the interface.

NotificationProxy

Now we are going to create two implementation classes, one for e-mail notification and the other for SMS notification.

SMSNotificationProxy
EmailNotificationProxy

As we have seen for Beans, a class can also be named using the @Qualifier notation. This is critical because unless we use the @Primary notation Spring would not know which of the two implementations (email, SMS) to take.

CREATION OF THE SERVICE PACKAGE

We create a new package, service. This is a fundamental package in which all the interfaces and related implementations that represent the Business logic are to be placed. Let’s go on to create the ClientiService interface that will have the purpose of saving the customer and sending him the notification.We create the ClientiServiceImpl class where we will see the interaction between two different components at work.

ClientiService
ClientiServiceImpl

THE CODE INJECTION

Here we need to implement Code Injection of previously created elements, repository and notification. As I have already told you Code Injection can come in two ways: The first, commented out, done via constructor, where mind you this is where it is always specified with the @Qualifier notation which notification implementation we want to use.

THE @AUTOWIRED NOTATION

The second via the @Autowired notation. Again since this class must mandatorily be placed in the Context. I used the notation @Service instead of @Component to better clarify the role played by this class. Mind you, we use interfaces to ensure abstraction.

NB: It is essential to understand that through the @Qualifier notation we can choose which notification implementation we should send to the Client, (SMS,E-MAIL) all this without intervening in the code but only in the notation.

Now it is time to use all the work done so far by going to the Main entry point and writing the related code that I report.

Main

CONCLUSIONS

Run the application and view the printout in the console.

DOWNLOAD ARTICLE CODE

ITEM CODE

Below is the video for importing into your project workspace.

THE JAVA LANGUAGE

THE JAVA LANGUAGE

LINKS TO PREVIOUS POSTS

SPRING FRAMEWORK