Spring Framework dependency injection
Spring core
We will discuss the following topic.
Spring IOC
Dependency Injection
Spring Framework benefits in the form of Spring IOC.
Spring framework says you are a developer, so don’t waste your time creating and managing the object.
I can create objects for you.
I can manage objects for you and help our application to be Configurable.
As well it’s managing dependencies also.
So, we can say spring relay helps us manage the time as well.
Basically IOC provides to us – Without touching source code we can change the implementation using xml file.
It takes care of injecting the dependency also.
IOC Container – Spring having own container
Below is the diagram that shows if we create entities in an xml file for class a , b, c according to the spring container will create objects for us to use.
Types of IOC Container
How actually, it’s creating objects internally using Application Context Type Container.
Example Spring IOC-
Step by step
First we will create Interface - SIM
package com.javaupgradIoc.com; public interface Sim { void calling(); void data(); }
Second we will create two classes and implement Sim interface
package com.javaupgradIoc.com; public class RelianceJIO implements Sim{ @Override public void calling() { System.out.println("calling using Reliance sim"); } @Override public void data() { // TODO Auto-generated method stub System.out.println("Internet using Reliance sim"); } }
package com.javaupgradIoc.com; public class Airtel implements Sim{ public Airtel() { // TODO Auto-generated constructor stub System.out.println("Airtel constructor"); } @Override public void calling() { // TODO Auto-generated method stub System.out.println("Calling using airtel sim"); } @Override public void data() { // TODO Auto-generated method stub System.out.println("Internet using airtel sim"); } }
Now Create a main class and configurable class
package com.javaupgradIoc.com; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml"); System.out.println("config loaded"); Sim sim=context.getBean("sim",Sim.class);// using interface sim.calling(); sim.data(); } }
getBean() – method use for injecting the object through IOC container
beans.XML file name
IN xml based configuration , xml file supplies the metadata for the spring container ton manage the bean.
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="sim" class="com.javaupgradIoc.com.RelianceJIO"></bean>
</beans>
Its work internally look like - Sim sim=new RelianceJIO();
Sim sim=context.getBean("sim",Sim.class);// using interface we inject this id
OutPUT –
config loaded
calling using Reliance sim
Internet using Reliance sim
Spring Dependency Injection-
Spring will be going to inject our dependency.
In this diagram you can see class Family, class Job and collection in the form of numbers.
So these all are dependencies for Class me.
These all are dependencies
Please don’t do hard code – spring will going to inject your dependencies
Spring dependencies types
First Example using setter injection
package com.javaupgrad.di; public class Student { private String studentName; public void setStudentName(String studentName) { this.studentName = studentName; } public void displayStudentInfo() { System.out.println("student name is "+studentName); } }
Now we will create Exam class or configurable class
package com.javaupgrad.di; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.javaupgradIoc.com.Sim; public class Exam { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("beansdi.xml"); Student abhi=context.getBean("student", Student.class); abhi.displayStudentInfo(); } }
Then , we will create xml file
Note:-When we use setter type injection in spring, so that time we use Property tag
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="student" class="com.javaupgrad.di.Student">
<property name="studentName" value="Abhilash" />
</bean>
</beans>
OUtPUt
student name is Abhilash
Constructor type injection
First Example using Constructor injection
package com.javaupgrad.constructdi; public class Student { private int id; private String studentName; public Student(int id,String studentName){ System.out.println("constructor "); this.id=id; this.studentName=studentName; } public void displayStudentInfo() { // TODO Auto-generated method stub System.out.println("Student name is: " +studentName +" and the id is " +id); } }
package com.javaupgrad.constructdi; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.javaupgradIoc.com.Sim; public class Exam { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("beanscons.xml"); Student abhi=context.getBean("student", Student.class); abhi.displayStudentInfo(); } }
Note:-When we use setter type injection in spring, so that time we use Constructor tag
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="student" class="com.javaupgrad.constructdi.Student">
<constructor-arg name="studentName" value="abhilash" />
<constructor-arg name="id" value="1" />
</bean>
</beans>
No comments: