Types of Dependency Injection
1. Setter Injection
2. Constructor Injection
3. Interface Injection
Spring frame work only supports Setter and Constructor injections, but interface injection is not supported.
Interface injection is supported by struts 2.x
Setter Injection:-
1. It means Dependency of an object (or) Injected by calling setter method of that object.
2. In the spring bean class, a setter method is required for a property to inject its value by the spring container.
3. In setter Injection, for each property of the spring bean class, a setter method is required.
4. A spring container can come to know about setter injection in a bean class by using <property> tag in spring configuration file.
Example :-
// SpringBean
Public class DemoBean
{
Private String message;
// Setter Injection
Public void setMessage(String message);
{
This.message=message;
}
Public void show()
{
System.out.println(message)
}
}//class
Public class DemoBean
{
Private String message;
// Setter Injection
Public void setMessage(String message);
{
This.message=message;
}
Public void show()
{
System.out.println(message)
}
}//class
// Spring-config.xml
<beans>
<bean id= ”id1” ”DemoBean”>
<property name=”message”>
<value> Welcome to Spring</value>
</property>
</bean>
<beans>
<beans>
<bean id= ”id1” ”DemoBean”>
<property name=”message”>
<value> Welcome to Spring</value>
</property>
</bean>
<beans>
1. An object for us spring bean configure in a spring configuration file will be created by the Spring IOC container.
2. While creating bean object by the spring container, if any dependency are required then the container injects the dependencies using the configuration provided in xml file.
3. In the above xml file, the configure <property> tag, so spring container understands that there is a setter injection in the beans class for injecting the dependency value.
4. Internally the spring container generates the following code
DemoBean id1= new DemoBean();
Id1.setMessage(“Welcome to Spring”)
2. While creating bean object by the spring container, if any dependency are required then the container injects the dependencies using the configuration provided in xml file.
3. In the above xml file, the configure <property> tag, so spring container understands that there is a setter injection in the beans class for injecting the dependency value.
4. Internally the spring container generates the following code
DemoBean id1= new DemoBean();
Id1.setMessage(“Welcome to Spring”)
What is a Spring Container?
1. A spring container is a pre-defined Interface provided by spring –framework which takes care about the entire life cycle of spring beans.
2. In spring framework, spring container is identifies with an interface BeanFactory and this container takes care about the life to death of a spring bean.
3. Generally containers are called a special software application, which takes care about life cycle of some other applications.
For ex:-
A web container is a special software application which takes care about life cycle of servlets and jsp’s similarly a spring container takes care about the spring beans.
4. In spring framework spring container is also called spring IOC container and it is identify with the following two interfaces.
a. BeanFactory Interface (Basic Container)
b. ApplicationContext Interface (Advanced Container)
Steps to create a client application to call Business logic of a spring bean?
1. A client application is another java application, which wants to get Business logic service of a SpringBean. In this case a client application needs an object of spring bean.
2. A client application can be a stand alone application (or) Desktop application (or) Servlet (or) ejb etc…
Ex: -
If a servlet is called Business logic of a spring bean then that servlet is called a client application for spring bean.
3. For a client application, spring bean objects are provided by spring container so a client application first need spring container object.
Step: 1
a. Locate spring configuration file
b. To locate the spring configuration file we need to create a Resource object.
c. In spring framework resource is an interface and classpathResource is an implementation class, both are given in either package name org.springframework.core.io package.
Syntax:
Resource res= new classpathResource (“spring-configure.xml”);
Step: 2
a. Activate that, is create an object of spring container.
b. Spring container is an identified with BeanFactory interface and we need an object of its implementation class called xmlBeanFactory class.
c. BeanFactory is an interface of org.springframework.beans.factory package
d. xmlBeanFactory is a class of org.springframework.beans.factory.xml package
Syntax: -
BeanFactory factory = new xmlBeanFactory (res);
Step: 3
a. Get a required spring bean object from spring container
b. To get a bean object from container we need to call getBean() by taking id of a bean as a parameter.
c. getBean() returns a spring bean method in the form of object class object, so we need to type cast in to our spring bean type.
Syntax: -
Object o = factory.getBean(“id1”);
DemoBean db= (DemoBean)0;
Step: 4
To call Business method of a spring bean
Syntax: -
Db.show();
Why getBean() of a spring container returns Object class object?
1. Spring Container is responsible for creating any spring bean class object, configure in the xml file. So after creating that object, getBean method returns that bean object in the form of Object class. The reason is Object is a base class for all class in java.
2. The getBean() of spring container is designed to create any spring bean object and to return that object, so while creating getBean() method, the framework developers used return type of getBean() as object.
2. A client application can be a stand alone application (or) Desktop application (or) Servlet (or) ejb etc…
Ex: -
If a servlet is called Business logic of a spring bean then that servlet is called a client application for spring bean.
3. For a client application, spring bean objects are provided by spring container so a client application first need spring container object.
Step: 1
a. Locate spring configuration file
b. To locate the spring configuration file we need to create a Resource object.
c. In spring framework resource is an interface and classpathResource is an implementation class, both are given in either package name org.springframework.core.io package.
Syntax:
Resource res= new classpathResource (“spring-configure.xml”);
Step: 2
a. Activate that, is create an object of spring container.
b. Spring container is an identified with BeanFactory interface and we need an object of its implementation class called xmlBeanFactory class.
c. BeanFactory is an interface of org.springframework.beans.factory package
d. xmlBeanFactory is a class of org.springframework.beans.factory.xml package
Syntax: -
BeanFactory factory = new xmlBeanFactory (res);
Step: 3
a. Get a required spring bean object from spring container
b. To get a bean object from container we need to call getBean() by taking id of a bean as a parameter.
c. getBean() returns a spring bean method in the form of object class object, so we need to type cast in to our spring bean type.
Syntax: -
Object o = factory.getBean(“id1”);
DemoBean db= (DemoBean)0;
Step: 4
To call Business method of a spring bean
Syntax: -
Db.show();
Why getBean() of a spring container returns Object class object?
1. Spring Container is responsible for creating any spring bean class object, configure in the xml file. So after creating that object, getBean method returns that bean object in the form of Object class. The reason is Object is a base class for all class in java.
2. The getBean() of spring container is designed to create any spring bean object and to return that object, so while creating getBean() method, the framework developers used return type of getBean() as object.
Constructor Injection: -
If dependency of an object is injected by calling constructor of the class then the injection is called a Constructor Injection.
While configuring spring bean in Spring configuration file, we need to configure <constructor-arg> tag under <bean> tag.
For ex: -
// WelcomeBean
Public class WelcomeBean
{
Private string message;
//constructor injection
Public WelcomeBean(string Message)
{
This.message=message;
}
Public void show()
{
System.out.println(message);
}
}//class
//spring-config.xml
<beans>
<bean id=”id1” ”WelcomeBean”>
<constructor-arg Value=”Welcome to Spring”/>
</bean>
</beans>
Note: -
In <constructor-arg > tag, name attribute is not available it contains Value attribute.
If a constructor has multiple parameter then we need to configure <constructor-arg> tag for multiple times.
While configuring spring bean in Spring configuration file, we need to configure <constructor-arg> tag under <bean> tag.
For ex: -
// WelcomeBean
Public class WelcomeBean
{
Private string message;
//constructor injection
Public WelcomeBean(string Message)
{
This.message=message;
}
Public void show()
{
System.out.println(message);
}
}//class
//spring-config.xml
<beans>
<bean id=”id1” ”WelcomeBean”>
<constructor-arg Value=”Welcome to Spring”/>
</bean>
</beans>
Note: -
In <constructor-arg > tag, name attribute is not available it contains Value attribute.
If a constructor has multiple parameter then we need to configure <constructor-arg> tag for multiple times.
Visitors