Spring – Injecting Literal Values By Setter Injection
Spring IoC (Inversion of Control) Container is the core of Spring Framework. It creates the objects, configures and assembles their dependencies, manages their entire life cycle. The Container uses Dependency Injection(DI) to manage the components that make up the application. It gets the information about the objects from a configuration file(XML) or Java Code or Java Annotations and Java POJO class. These objects are called Beans. Since the Controlling of Java objects and their lifecycle is not done by the developers, hence the name Inversion Of Control. The followings are some of the main features of Spring IoC,
- Creating Object for us,
- Managing our objects,
- Helping our application to be configurable,
- Managing dependencies
Spring Dependency Injection
Dependency Injection is the main functionality provided by Spring IOC(Inversion of Control). The Spring-Core module is responsible for injecting dependencies through either Constructor or Setter methods. The design principle of Inversion of Control emphasizes keeping the Java classes independent of each other and the container frees them from object creation and maintenance. These classes, managed by Spring, must adhere to the standard definition of Java-Bean. Dependency Injection in Spring also ensures loose coupling between the classes. There are two types of Spring Dependency Injection.
- Setter Dependency Injection (SDI)
- Constructor Dependency Injection (CDI)
To read more on Spring Dependency Injection please refer to this article: Spring Dependency Injection with Example
Setter Injection is the simpler of the two Dependency Injection methods. In this, the Dependency Injection will be injected with the help of setter and/or getter methods. Now to set the Dependency Injection as Setter Injection in the bean, it is done through the bean-configuration file For this, the property to be set with the Setter Injection is declared under the <property> tag in the bean-config file.
So in this article, let’s learn how we are going to use Spring to inject our dependencies into our literal values by Setter Injection. Literals in Java are a synthetic representation of boolean, numeric, character, or string data. It is a medium of expressing particular values in the program, such as an integer variable named ‘’/count is assigned an integer value in the following statement.
int x = 100; // Here 100 is a constant/literal.
Illustration: String literal.
String s = "Hello";
Example: Create a simple class Student having two attributes id and studentName. Create setter methods for these two attributes and a simple method to print the details of the student.
Now let’s create a student Bean in the beans.xml file and inside the bean, you have to add your property’s name and its corresponding values inside the <property> tag, like this
<bean id="AnyUniqueId" class="YourClassName"> <property name="attributes that you have defined in your class" value="And its corresponding values"/> </bean>
For example, for this project, we can write something like this
<bean id="studentAmiya" class="Student"> <property name="id" value="101"/> <property name="studentName" value="Amiya Rout"/> </bean>
Similarly, we can create another bean and put the values like this
<bean id="studentAsish" class="Student"> <property name="id" value="102"/> <property name="studentName" value="Asish Rout"/> </bean>
So below is the complete code for the beans.xml file
So now our two beans are ready. Now let’s create a class and define the main() method inside that class.
Suppose we have created a class name Exam and we have defined the main() method inside this class. Below is the code for the Exam.java class. Comments are added inside the code for better understanding.
Now run your main() method and the output will be like this.
Student Name is Amiya Rout and Roll Number is 101 Student Name is Asish Rout and Roll Number is 102
Please Login to comment...