Spring IoC (Inversion of Control)
Spring IoC (Inversion of Control)
Spring IoC: IoC is a principle in which an external agency/entity implicitly providing collaboration to application components instead of components looking up for them.
IoC provides the loose coupling between object. Ioc principle is famously known as Hollywood principle "don't call me, I will call you."
Q. What is DI(Dependency Injection)?
A. Dependency Injection is a design pattern which implement IoC principle.dependency injection is basically providing the objects that an object need instead of create them itself.
-> Dependencies are injected into Dependent through dependency injection.
-> In the context of spring application spring container providing one layer object reference to the previous layer object implicitly is nothing but dependency injection.
We have 3 types of dependency injection in spring:
1. setter injection.
2. constructor injection.
3. Interface injection (not yet implemented in spring).
Q. Explain about the setter injection.
A. Spring container providing the dependency (reference) to dependent by implicitly invoking the dependents setter method is nothing but setter injection. In a spring application
the following steps are involved in implementing setter injection.
Step 1: Declare the dependency as instance variable in dependent class.
for example: public class AccountService{
private AccountDAO accountDao;
}
Step 2: Implement the setter method in dependent class.
for example: public class AccountService{
private AccountDAO accountDao;
public void setAccountDao(AccountDAO acdao){
accountDao = acdao;
}
}
Step 3: ensure that dependent has a no argument constructor.
Step 4: Make use of <property> tag in spring configuration file as sub tag in dependent bean definition.
Q. Develop a spring application that displays "Hello Spring World" on the console using setter injection.
A. helloworldapplication
|->HelloWorldBean.java
|->beans.xml
|->SpringApplication.java
HelloWorldBean.java
-------------------
package com.vivek;
public class HelloWorldBean{
private String message;
public void setMessage(String message){
this.message = message;
}
public void displayMessage(){
System.out.println(message);
}
}
bean.xml
--------
<beans>
<bean id="hwb" class="com.vivek.HelloWorldBean">
<property name="message" value="Hello Spring World"/>
</bean>
</beans>
SpringApplication.java
----------------------
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import com.vivek.spring.HelloWorldBean;
class SpringApplication{
public static void main(String[] args){
XmlBeanFactory factory = new XmlBeanFactory(FileSystemResource("beans.xml"));
HelloWorldBean bean = (HelloWorldBean)factory.getBean("hwb");
bean.displayMessage();
}
}
Q. Explain about the constructor injection.
A. If Spring container provides dependencies to dependent by invoking parameterised constructor of the dependent beans, it is known as constructor injection.
the following steps are involved in implementing setter injection.
Step 1: Declare the dependency as instance variable in dependent class.
for example: public class AccountService{
private AccountDAO accountDao;
}
Step 2: Implement parametrised constructor in dependent class.
for example: public class AccountService{
private AccountDAO accountDao;
public AccountService(AccountDAO accountDao){
this.accountDao = accountDao();
}
}
Step 3: Make use of <constructor-arg> as sub tag within dependent bean definition.
Q. Develop a spring application that displays "Hello Spring World" on the console using constructor injection.
A. helloworldapplication
|->HelloWorldBean.java
|->beans.xml
|->SpringApplication.java
HelloWorldBean.java
-------------------
package com.vivek;
public class HelloWorldBean{
private String message;
public HelloWorldBean(String message){
this.message = message;
}
public void displayMessage(){
System.out.println(message);
}
}
bean.xml
--------
<beans>
<bean id="hwb" class="com.vivek.HelloWorldBean">
<constructor-arg value="Hello Spring World"/>
</bean>
</beans>
SpringApplication.java
----------------------
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import com.vivek.spring.HelloWorldBean;
class SpringApplication{
public static void main(String[] args){
XmlBeanFactory factory = new XmlBeanFactory(FileSystemResource("beans.xml"));
HelloWorldBean bean = (HelloWorldBean)factory.getBean("hwb");
bean.displayMessage();
}
}
Spring IoC: IoC is a principle in which an external agency/entity implicitly providing collaboration to application components instead of components looking up for them.
IoC provides the loose coupling between object. Ioc principle is famously known as Hollywood principle "don't call me, I will call you."
Q. What is DI(Dependency Injection)?
A. Dependency Injection is a design pattern which implement IoC principle.dependency injection is basically providing the objects that an object need instead of create them itself.
-> Dependencies are injected into Dependent through dependency injection.
-> In the context of spring application spring container providing one layer object reference to the previous layer object implicitly is nothing but dependency injection.
We have 3 types of dependency injection in spring:
1. setter injection.
2. constructor injection.
3. Interface injection (not yet implemented in spring).
Q. Explain about the setter injection.
A. Spring container providing the dependency (reference) to dependent by implicitly invoking the dependents setter method is nothing but setter injection. In a spring application
the following steps are involved in implementing setter injection.
Step 1: Declare the dependency as instance variable in dependent class.
for example: public class AccountService{
private AccountDAO accountDao;
}
Step 2: Implement the setter method in dependent class.
for example: public class AccountService{
private AccountDAO accountDao;
public void setAccountDao(AccountDAO acdao){
accountDao = acdao;
}
}
Step 3: ensure that dependent has a no argument constructor.
Step 4: Make use of <property> tag in spring configuration file as sub tag in dependent bean definition.
Q. Develop a spring application that displays "Hello Spring World" on the console using setter injection.
A. helloworldapplication
|->HelloWorldBean.java
|->beans.xml
|->SpringApplication.java
HelloWorldBean.java
-------------------
package com.vivek;
public class HelloWorldBean{
private String message;
public void setMessage(String message){
this.message = message;
}
public void displayMessage(){
System.out.println(message);
}
}
bean.xml
--------
<beans>
<bean id="hwb" class="com.vivek.HelloWorldBean">
<property name="message" value="Hello Spring World"/>
</bean>
</beans>
SpringApplication.java
----------------------
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import com.vivek.spring.HelloWorldBean;
class SpringApplication{
public static void main(String[] args){
XmlBeanFactory factory = new XmlBeanFactory(FileSystemResource("beans.xml"));
HelloWorldBean bean = (HelloWorldBean)factory.getBean("hwb");
bean.displayMessage();
}
}
Q. Explain about the constructor injection.
A. If Spring container provides dependencies to dependent by invoking parameterised constructor of the dependent beans, it is known as constructor injection.
the following steps are involved in implementing setter injection.
Step 1: Declare the dependency as instance variable in dependent class.
for example: public class AccountService{
private AccountDAO accountDao;
}
Step 2: Implement parametrised constructor in dependent class.
for example: public class AccountService{
private AccountDAO accountDao;
public AccountService(AccountDAO accountDao){
this.accountDao = accountDao();
}
}
Step 3: Make use of <constructor-arg> as sub tag within dependent bean definition.
Q. Develop a spring application that displays "Hello Spring World" on the console using constructor injection.
A. helloworldapplication
|->HelloWorldBean.java
|->beans.xml
|->SpringApplication.java
HelloWorldBean.java
-------------------
package com.vivek;
public class HelloWorldBean{
private String message;
public HelloWorldBean(String message){
this.message = message;
}
public void displayMessage(){
System.out.println(message);
}
}
bean.xml
--------
<beans>
<bean id="hwb" class="com.vivek.HelloWorldBean">
<constructor-arg value="Hello Spring World"/>
</bean>
</beans>
SpringApplication.java
----------------------
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import com.vivek.spring.HelloWorldBean;
class SpringApplication{
public static void main(String[] args){
XmlBeanFactory factory = new XmlBeanFactory(FileSystemResource("beans.xml"));
HelloWorldBean bean = (HelloWorldBean)factory.getBean("hwb");
bean.displayMessage();
}
}

Comments
Post a Comment