Basic principle behind DI or IOC is that objects define their dependencies. It is the job of container to actually inject those dependencies when it creates bean. The basic concept of DI is that, you don't need to create your objects but describe how they should be created.. You don't directly connect your components and services together in code but describe which services are needed by which component in a configuration file.
There are two types of dependency Injection :
1) Setter Injection
2) Constructor Injection
1) Setter Injection :
The Spring IoC container also supports setter injection, which is the preferred method of dependency injection in Spring.
Setter injection uses the
Project Structure :
Example :
DrawingApp.java
package com.example1;
setter
methods in a class file to garner property names that are configurable in the spring XML config. Let's take one example.Project Structure :
Example :
DrawingApp.java
package com.example1;
public class DrawingApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle = (Triangle) context.getBean("triangle");
triangle.draw();
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle = (Triangle) context.getBean("triangle");
triangle.draw();
}
}
}
Triangle.java (Bean)
package com.example1;
public class Triangle {
private String type;
private int height;
private String type;
private int height;
public String getType() {
return type;
}
public void setType(String type) { // Setter Injection(called in order to initialize the value)
this.type = type;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public void draw(){
System.out.println("Triangle Drawn :" +getType()+" with Height :" +getHeight());
}
}
return type;
}
public void setType(String type) { // Setter Injection(called in order to initialize the value)
this.type = type;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public void draw(){
System.out.println("Triangle Drawn :" +getType()+" with Height :" +getHeight());
}
}
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="triangle" class="com.example1.Triangle">
<property name="type" value="Equilateral" />
<property name="height" value="65" />
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="triangle" class="com.example1.Triangle">
<property name="type" value="Equilateral" />
<property name="height" value="65" />
</bean>
</beans>
</beans>
Output : Equilateral Triangle Drawn with Height 65
2) Constructor Injection :
As the name implies, using constructor the Spring IOC container will inject the dependencies. The constructor will
take arguments based on number of dependencies required. This is one of the drawback using Constructor based DI.
You don't have option to reconfigure the dependencies at later point of time, since all the dependencies are resolved
only at the time of invoking the constructor. if you don't have requirement to inject all the dependencies, please
use Setter Injection technique to obtain the more flexibilty on configuring beans.
Example :
DrawingApp.java
package com.example1;
public class DrawingApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle = (Triangle) context.getBean("triangle");
triangle.draw();
}
}
Triangle.java
package com.example1;
public class Triangle {
private String type;
private int height;
public Triangle(String type, int height){
this.type = type;
this.height = height;
}
public void draw(){
System.out.println(this.type+" Triangle Drawn with height " +this.height);
}
}
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="triangle" class="com.example1.Triangle">
<constructor-arg value="Equilateral" />
<constructor-arg value="65" />
</bean>
</beans>
Output : Equilateral Triangle Drawn with height 65
To execute this examples you need jar files which you can download from http://www.springsource.org/download.
Lucky Club Casino Site - Play Slots, Blackjack & More
ReplyDeleteLucky Club is a leading gambling site luckyclub in Europe and Asia. Our innovative and innovative casino games allow you to enjoy the casino in your own