Aegis Java web development team is all set to share this tutorial with global java web development community to make developers efficient in understanding how spring manages the several objects using beans configuration. With the help of simple java code snippet (written in the post), you can test the configuration used in this post.
Java web application generally does not consist of a single object, it always will have a multiple objects depending on each other to achieve some output. Even a simple java application will have few objects that works together to generate some output.
Here in this section we will see how spring manages the number of objects using beans configuration.
Don't make yourself confuse here, IoC (Inversion of Control) and DI (Dependency Injection) both are nothing but a same things. We refer to the same configuration calling it by two different names.
In simple word DI is a process that let the user free from worrying about new keyword. Don't need to take care of new keyword in your project to initialize a depending object, let the spring DI manage this things.
Let’s jump in to the visible example to understand this concept rather than confusing yourself with theoretical concepts.
As we can see the above, PersonModel object contains dependent object ContactModel and we initialize it through new keyword in constructor of PersonModel class.
Here Spring comes into the picture to resolve above dependency there are two types of dependency injections available.
Constructor-based DI is accomplished by the container to invokes a class constructor with matching number of constructor arguments.
Lets consider above class.
Below is the spring configuration file that has been declared using constructor-args for the above class.
Now lets add one more class that have the dependent object for the constructor based DI
Here we will consider ContactModel same as above.
As you can see the fact we provided the reference of the bean and excluded use of new keyword, spring itself will manage the same.
Setter-based DI is accomplished by the container to invoke setter methods of POJO class after default (no-args) constructor.
Lets have an example to understand it better.
As you can see we removed the parameterized constructor and add getter/setter method to the pojo.
You should note the difference in config.xml file between constructor-based injection and setter-based injection.
The only difference is the element <constructor-arg> for constructor-based injection and <property> for setter-based injection.
Second most important point to be noted here is in case of initializing dependent object, it is required to use ref attribute of <property> tag.
Lets have an example adding one more class to out structure.
Please note the difference; we have ContactModel as a child object. Please see the configuration for such scenario.
Once done with the above concept here I would like to share simple java code snippet to test the above configuration.
DONE... Just have to resolve the dependencies by including jars.
Hope this tutorial has helped you in understanding the use of spring for managing number of objects with beans configuration in java web development. You can ask anything related to this post from our java web development team that ensures flawless response to commenter.
For further information, mail us at firstname.lastname@example.org