0% found this document useful (0 votes)
87 views

Spring Core

The document discusses the Spring Framework. Some key points: - Spring is a lightweight, open-source framework that supports Java EE applications. It includes modules for IOC, AOP, MVC, and connecting to data access technologies. - IOC and dependency injection decouple classes by managing object dependencies externally through metadata rather than hard-coding them. This improves testability and maintainability. - The IoC container manages the lifecycle and configuration of application objects. It handles dependency injection through constructor or setter methods defined in XML or annotations.

Uploaded by

asma sayyad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Spring Core

The document discusses the Spring Framework. Some key points: - Spring is a lightweight, open-source framework that supports Java EE applications. It includes modules for IOC, AOP, MVC, and connecting to data access technologies. - IOC and dependency injection decouple classes by managing object dependencies externally through metadata rather than hard-coding them. This improves testability and maintainability. - The IoC container manages the lifecycle and configuration of application objects. It handles dependency injection through constructor or setter methods defined in XML or annotations.

Uploaded by

asma sayyad
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Spring Framework

Spring Framework
 Spring is a lightweight framework.
 It can be thought of as a framework of frameworks because it provides
support to various frameworks such as Struts, Hibernate, Tapestry, EJB, JSF,
etc.
 The framework, in broader sense, can be defined as a structure where we
find solution of the various technical problems.
 The Spring framework comprises several modules such as IOC, AOP, DAO,
Context, ORM, WEB MVC etc.
Inversion Of Control (IOC) and Dependency Injection

 These are the design patterns that are used to remove dependency from the
programming code.
 They make the code easier to test and maintain.
 IOC makes the code loosely coupled. In such case, there is no need to modify
the code if our logic is moved to new environment.
 In Spring framework, IOC container is responsible to inject the dependency.
 We provide metadata to the IOC container either by XML file or annotation.
 Advantage of Dependency Injection :
 makes the code loosely coupled so easy to maintain
 makes the code easy to test
Advantages of Spring Framework
 Predefined Templates :
Spring framework provides templates for JDBC, Hibernate, JPA etc.
technologies. So there is no need to write too much code. It hides the basic
steps of these technologies.

 Loose Coupling :
The Spring applications are loosely coupled because of dependency injection.

 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.
Advantages of Spring Framework
 Lightweight :
Spring framework is lightweight because of its POJO implementation.
The Spring Framework doesn't force the programmer to inherit any class
or implement any interface. That is why it is said non-invasive.
 Fast Development :
The Dependency Injection feature of Spring Framework and it support to
various frameworks makes the easy development of JavaEE application.
 Powerful abstraction :
It provides powerful abstraction to JavaEE specifications such as JMS, JDBC,
JPA and JTA.
 Declarative support :
It provides declarative support for caching, validation, transactions and
formatting.
Spring Modules
Creating spring application in Eclipse IDE
 Let's see the simple steps to create the spring application in Eclipse IDE :

 create the java project


 add spring jar files
 create the class
 create the xml file to provide the values
 create the test class
IoC Container
 The IoC container is responsible to instantiate, configure and assemble the
objects.
 The IoC container gets information from the XML file and works accordingly.
 The main tasks performed by IoC container are :
 to instantiate the application class
 to configure the object
 to assemble the dependencies between the objects
 There are two types of IoC containers :
 BeanFactory
 ApplicationContext
Difference between BeanFactory and the ApplicationContext

 The org.springframework.beans.factory.BeanFactory and the


org.springframework.context.ApplicationContext interfaces acts as the IoC
container.
 The ApplicationContext interface is built on top of the BeanFactory interface.
 It adds some extra functionality than BeanFactory such as simple integration
with Spring's AOP, message resource handling (for I18N), event propagation,
application layer specific context (e.g. WebApplicationContext) for web
application.
 So it is better to use ApplicationContext than BeanFactory.
Dependency Injection in Spring
 Dependency Injection (DI) is a design pattern that removes the dependency
from the programming code so that it can be easy to manage and test the
application.
 In such case we provide the information from the external source such as XML
file.
 Dependency Injection makes our programming code loosely coupled and
easier for testing.
 Spring framework provides two ways to inject dependency :
 By Constructor
 By Setter method
Dependency Injection by Constructor
 We can inject the dependency by constructor.
 The <constructor-arg> subelement of <bean> is used for constructor injection.
 We can inject :
 primitive and String-based values
 Dependent object (contained object)
 Collection values : There can be used three elements inside the constructor-arg element.
It can be:
1. list
2. set
3. map
Dependency Injection by setter method
 We can inject the dependency by setter method also. The <property>
subelement of <bean> is used for setter injection.
 We can inject :
 primitive and String-based values
 Dependent object (contained object)
 Collection values : There can be used three elements inside the property element.
It can be:
1. list
2. set
3. map
Difference between constructor and setter injection

 There are many key differences between constructor injection and setter
injection.
 Partial dependency: can be injected using setter injection but it is not
possible by constructor. Suppose there are 3 properties in a class, having 3
arg constructor and setters methods. In such case, if you want to pass
information for only one property, it is possible by setter method only.
 Overriding: Setter injection overrides the constructor injection. If we use
both constructor and setter injection, IOC container will use the setter
injection.
 Changes: We can easily change the value by setter injection. It doesn't create
a new bean instance always like constructor. So setter injection is flexible
than constructor injection.
Autowiring in Spring
 Autowiring feature of spring framework enables you to inject the object
dependency implicitly. It internally uses setter or constructor injection.
 Autowiring can't be used to inject primitive and string values. It works with
reference only.
 Advantage of Autowiring :
 It requires the less code because we don't need to write the code to inject the
dependency explicitly.
 Disadvantage of Autowiring :
 No control of programmer.
 It can't be used for primitive and string values.
Autowiring Modes
 no : It is the default autowiring mode. It means no autowiring bydefault.
 byName : The byName mode injects the object dependency according to
name of the bean. In such case, property name and bean name must be same.
It internally calls setter method.
 byType : The byType mode injects the object dependency according to type.
So property name and bean name can be different. It internally calls setter
method.
 constructor : The constructor mode injects the dependency by calling the
constructor of the class. It calls the constructor having large number of
parameters.
 You need to use autowire attribute of bean element to apply the autowire
modes.

You might also like