2. IOC and Dependency Injection
1. Dependency Injection is the main functionality provided by Spring
IOC (Inversion of Control).
2. The Spring-Core module is responsible for injecting dependencies
through either Constructor or Setter methods in POJO class.
3. Dependency Injection in Spring also ensures loose-coupling
between the classes.
Need for Dependency Injection:
1. Spring IOC resolves dependencies with Dependency Injection,
which makes the code easier to test and reuse.
2. Loose coupling between classes can be possible by defining
interfaces for common functionality and the injector will instantiate
the objects of required implementation.
3.
4. Example
Tight Coupling - (without IOC)
class Employee{
Address address;
Employee()
{address=new Address(“xyz
street”, ”Lucknow”, ”UP”);
}}
class Address{
Address(String street, String city,
String state){
……………………..
...............................
}}
One class object is created in another class in tight coupling. Leads to
a. Stop server b. Recompile c. Create jar and war file
d. Redeploy e. Restart the server.
5. 1. with IOC (Setter Injection)
class Employee{
Address address;
public void setAddress(Address
address)
{
this.address=address;
} }
class Address
{
Address()
{
……………………….
……………………….
}}
Loose Coupling
6. 2. with IOC (Constructor Injection)
class Employee
{
Address address;
Employee(Address address)
{
this.address=address;
} }
class Address
{
Address()
{
……………………….
……………………….
}}
No need to stop the existing component, in real time it is better.
7. POJO(Plain Old Java Object)
1. POJO is a ordinary Java Object not bounded by any restriction,
they are loosely coupled.
2. POJO help to increase the readability and reusability of a program.
3. Helps to achieve three types of Dependencies
1. Dependencies in the form of Primitives
a. Setter Dependency
b. Constructor Dependency
2. Dependency in the form of Object
3. Dependency in the form of Collections
8. Rules for Creating POJO‘s
1. Class must be public
2. Variables must be private
3. Must having public default Constructor
4. Can have arg-constructor
5. Every property should have public getter and setter method
9. POJO Example
public class Employee
{
private String name,email;
public String getName()
{return name;
}
public void setName(String name)
{this.name=name;}
public String getEmail()
{return email;}
public void setEmail(String email)
{this.email=email;}
}
10. Containers in Spring
1. Core Container (slower)
a. It is called Lazy Container.
b. It create object on demand one by one.
c. It will create the object when the bean file is loaded.
ClassPathResource res=new ClassPathResource(“Spring.xml”);
XmlBeanFactory fact=new XmlBeanFactory(res);
fact.getBean(“abc”);
2. J2EE Container (faster)
a. It is called Eager Container.
b. It create all object at once.
c. It cerate the object as soon as object of ApplicationContext is made.
ApplicationContext con=new
ClassPathXmlApplicationContext(“Spring.xml”);
13. 1. The objects that form the backbone of your application and that are
managed by the Spring IoC container are called beans.
2. A bean is an object that is instantiated, assembled, and otherwise
managed by a Spring IoC container.
3. These beans are created with the configuration metadata that you
supply to the container. For example, in the form of XML <bean/>
definitions.
4. Bean definition contains the information called configuration
metadata, which is needed for the container to know the following −
a. How to create a bean
b. Bean's lifecycle details
c. Bean's dependencies
Bean File (.xml)
14. Properties & Description in Bean File
1. class : This attribute is mandatory and specifies the bean class to
be used to create the bean.
2. name: This attribute specifies the bean identifier uniquely. In XML
based configuration metadata, you use the id and/or name
attributes to specify the bean identifier(s).
3. scope: This attribute specifies the scope of the objects created from
a particular bean definition.
4. constructor-arg: This is used to inject the dependencies.
5. properties: This is used to inject the dependencies.
6. autowiring mode: This is used to inject the dependencies.
15. 7. lazy-initialization mode: A lazy-initialized bean tells the IoC
container to create a bean instance when it is first requested, rather
than at the startup.
8. initialization method: A callback to be called just after all necessary
properties on the bean have been set by the container.
9. destruction method: A callback to be used when the container
containing the bean is destroyed.
16. Bean Scope
When defining a <bean> you have the option of declaring a scope for that
bean. For example, to force Spring to produce a new bean instance each
time, you should declare the bean's scope attribute to be prototype.
Similarly, if you want Spring to return the same bean instance each time
one is needed, you should declare the bean's scope attribute to
be singleton(default).
Scope & Description
1. Singleton: This scopes the bean definition to a single instance per
Spring IoC container (default).
2. Prototype: This scopes a single bean definition to have any number
of object instances.
17. Bean Lifecycle
1. When a bean is instantiated, it may be required to perform some
initialization to get it into a usable state. Similarly, when the bean is
no longer required and is removed from the container, some
cleanup may be required.
2. The time of bean Instantiation and its destruction, bean life cycle
callback methods, which are required at the time of bean
initialization and its destruction.
3. To define setup and teardown for a bean, we simply declare the
<bean> with init method and/or destroy-method parameters. The
init-method attribute specifies a method that is to be called on the
bean immediately upon instantiation and destroy method specifies
a method that is called just before a bean is removed from the
container.
18. How to Start Using Spring
1. create the class
2. create the xml file to provide the values
3. create the test class
4. Load the spring jar files
5. Run the test class
22. 4. Load the jar files required for spring framework
There are mainly three jar files required to run this application.
1. org.springframework.core-3.0.1.RELEASE-A
2. com.springsource.org.apache.commons.logging-1.1.1
3. org.springframework.beans-3.0.1.RELEASE-A
5. Run the TEST FILE
26. Advantages of POJO’s
1. Lightweight - Spring framework is lightweight because of its POJO
Enable you to write powerful, scalable applications using POJO.
2. Easy to develop J2EE application - The Dependency Injection
feature of Spring Framework and it support to various frameworks
makes the easy development of JavaEE application.
3. Easy to test - The Dependency Injection makes easier to test the
application. The EJB or Struts application require server to run the
application but Spring framework doesn't require server.
4. Loose Coupling - The Spring applications are loosely coupled
because of dependency injection and handles injecting dependent
components without a component knowing where they came from
(IoC).
27. 8. Lifecycle – Spring Framework is responsible for managing POJO’s
lifecycle: init(), destroy().