Earlier we have learnt about Dependency Injection in Spring and its types. Let’s now take a deeper look into them one by one. We will start with Setter Injection with Primitive values.
Folder Structure:
- Create a new Java Project “SpringCoreTutorial” and create a package for our src files “com.javainterviewpoint“
- Add the required libraries to the build path. Java Build Path ->Libraries ->Add External JARs and add the below jars.
commons-logging-1.1.1.jar
spring-beans-3.2.9.RELEASE.jar
spring-core-3.2.9.RELEASE.jar
spring-context-3.2.9.RELEASE.jar
spring-expression-3.2.9.RELEASE.jar - Create the Java classes Employee.java and ClientLogic.java under com.javainterviewpoint folder.
- Place our configuration file SpringConfig.xml in the src directory
Employee.java
It is a simple java class containing the getters and setters of the employee details such as id, name and city. Whose values will be set through the configuration file and getEmployeeDetails() method prints the employee details which is set through the setter injection.
package com.javainterviewpoint; public class Employee { private int id; private String name; private String city; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public void getEmployeeDetails() { System.out.println("**Employee Details**"); System.out.println("ID : "+id); System.out.println("Name : "+name); System.out.println("City : "+city); } }
SpringConfig.xml
The SpringConfig.xml has the bean definition
- We have set bean id as “employee” for our Employee class which will act as the reference for calling our Employee class.
- Using <property> tag we have set the values to the properties in the Employee class(Setter Injection)
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="employee" class="com.javainterviewpoint.Employee"> <property name="id" value="123"></property> <property name="name" value="JavaInterviewPoint"></property> <property name="city" value="Chennai"></property> </bean> </beans>
ClientLogic.java
- In our ClientLogic class we will Read the Configuration file(SpringConfig.xml) and get all the bean definition through BeanFactory
- Get the Employee Class instance by calling the getBean() method over the bean factory.
- The String passed to getBean() method should be equivalent to the id defined in the SpringConfig.xml
- Call the getEmployeeDetails() method to display the values which we injected through the setter.
package com.javainterviewpoint; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class ClientLogic { public static void main(String args[]) { //Read the configuration file Resource resource = new ClassPathResource("SpringConfig.xml"); //Read all the bean definition BeanFactory bf = new XmlBeanFactory(resource); //Get the Student instance Employee employee = (Employee)bf.getBean("employee"); employee.getEmployeeDetails(); } }
Output
Once we run our ClientLogic.java we will get the below output
**Employee Details** ID : 123 Name : JavaInterviewPoint City : Chennai
Leave a Reply