INTRODUCTION TO CODE INJECTION
DISTINGUISH BEANS BY NAME AND PRIMARY NOTATION
Before discussing Code Injection let’s address other topics that are also fundamental to the Spring Framework. With the code in the previous article we saw that with a Bean everything is working. However, we have to wonder if instead of having one cliente, therefore one Bean there were two? Let’s test insert another Bean let’s call it cliente2 and see if Spring Context can resolve and distinguish between the two Beans. We also modify the Main class and see if everything works. I report the changes to the code.
If we try to execute this code we get an error as Spring cannot figure out which of the two Bean we intend to use.
When we have such a situation we need to add a name to our Beans. Once the Bean is named, in the getBean() method we need to add the identifier. I report the code.
In addition to identifying our Bean by name, we have the ability to specify which one is in a Primary state. The returned type being equal, the Bean with the @Primary notation will be taken as the reference. In the main we remove the name and you will see that the answer will be identical to the previous one. This is very important when for example we have Beans interfacing to databases, if we have two connections, with this notation we can specify the main database. Bean can also return native types such as a string. The downloadable code shows how this can be done.
USE THE @COMPONENTSCAN NOTATION
We modify the Clienti class by inserting the @Component notation, which is the most generic. In this case we are applying this notation to the Clienti class. Having done this we modify the ConfigApp configuration class code by removing the two Bean. I report the image.
With the @ComponentScan notation we are telling Spring go look in the main package for all the elements that are components that will have to go into the Context. The Clienti class since we used the @Component notation will be inserted into the Context as seen in the console window.
INTRODUCTION TO BEAN INTERACTION IN CONTEXT
The working principle of Spring is based on Inversion Of Control (IOC). The slide summarizes the concept.
We are going to add a new class, Bollini. I report all changes to the code.
It is apparent from the reported code that the Beans have been regularly created; however, the null value printed in the console indicates that there is still no interaction between the Bollini class and the Clienti class. In the Clienti class I am reporting, we have set up a Bollini property with its getters and setters; however, we are not telling it in any way how to get the Bean Bollini values when it is instantiated.
As a first step we modify the configuration class, passing as an argument the stamps in the Clienti Bean. By doing so now the two Bean can communicate with each other.
The Bollini class is instantiated only once, this is because Spring goes to check if that class is already present in the Context, if already present it will use that element, it will not go and instantiate another one. This means that in the Bean Clienti parameter will use the reference already instantiated. This is the first method to make the Bean talk to each other, a second is to remove the Bollini parameter and pass the bollini() method to the setter.
CODE INJECTION INTRODUCTION
We modify the ConfigApp class and add the @ComponentScan notation so that Spring is told in which packages to go to look for components that will be inserted into the Context. To do this we need to include the @Component notation in the relevant classes. Now in the Clienti class we are going to inject the code of the Bollini class.
There are two ways of Code Injection, the first is to pass in the constructor of the class Clienti the Bollini class and assign the parameter to a private property declared as final (see image above). The second is to use the @Autowired notation. Code Injection is one of the cornerstones of the Spring Framework.
DOWNLOAD ARTICLE CODE
THE JAVA LANGUAGE
LINKS TO PREVIOUS POSTS
HOW TO IMPORT PROJECTS INTO WINDOWS AND LINUX
To import projects, unzip the zipper file copy the code to your Workspace by making a switch of the Workspace itself to the Projects folder. The following video illustrates the procedure, which applies to both Windows and Linux.
Leave A Comment