Spring
Spring
Inversion of Control
1. Inversion of Control is a principle in software engineering which transfers the
control of objects or portions of a program to a container or framework.
2. The IoC container is responsible to instantiate the application class, configure the
object and assemble the dependencies between the objects. The IoC container
gets information from the XML file and works accordingly.
3. Two types of IoC containers. They are:
i. org.springframework.beans.factory.BeanFactory interface
ii. org.springframework.context.ApplicationContext interface
Eg: inversion-of-control-with-example
Dependency Injection
Dependency injection is a design pattern we can use to implement IoC, where the
control being inverted is setting an object's dependencies( “injecting” objects into
other objects).It removes the dependency from the programming code so that it can be
easy to manage and test the application. Dependency Injection makes our
programming code loosely coupled.Two ways to inject dependency: constructor and
setter method
Eg: spring-dependency-injection-with-example
Advantages of Spring
1. Predefined Template- Spring framework provides templates, so there is no need
to write too much code.Eg: Like JdbcTemplate, you don't need to write the code
for exception handling, creating connection, creating statement, committing
transaction, closing connection etc. You need to write the code of executing
query only.
2. 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.
3. Loose Coupling- The Spring applications are loosely coupled because of
dependency injection.
Constructor
1. The ref attribute is
used to define the
reference of another
object, such way we are
passing the dependent
object as an constructor
argument.
Setter Method
Autowiring
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.
Mode Description
byName
The byName mode injects the object dependency according to name of the bean. In suc
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.If you have multiple bean of one type, it will not work and
throw exception.
constructor The constructor mode injects the dependency by calling the constructor
of the class. It calls the constructor having large number of parameters.
no It is the default autowiring mode. It means no autowiring bydefault.
For better understanding
Aspect Oriented Programming (AOP)
Compliments OOPs in the sense that it also provides modularity( let the development
of software be divided into several components that can be implemented
simultaneously by the team of developers.). But the key unit of modularity is aspect
than class.
Let say of an example there are 5 methods that starts from m, 2 methods that starts
from n and 3 methods that starts from p.I have to maintain log and send notification
after calling methods that starts from m.
Problem without AOP We can call methods (that maintains log and sends notification)
from the methods starting with m. In such scenario, we need to write the code in all
the 5 methods.But, if client says in future, I don't have to send notification, you need
to change all the methods. It leads to the maintenance problem.
Solution with AOP Now we can define the additional concern like maintaining log,
sending notification etc. in the method of a class. Its entry is given in the xml file.In
future, if client says to remove the notifier functionality, we need to change only in
the xml file. So, maintenance is easy in AOP.
AOP concepts
Join Point- Any point in your program such as method execution, exception handling,
field access etc. Spring supports only method execution join point.
Weaving- It is the process of linking aspect with other application types or objects to
create an advised object. Weaving can be done at compile time, load time or runtime.
Spring AOP performs weaving at runtime.
Spring provides API to easily integrate Spring with Object Relational Mapping
(ORM)(is a technique used in creating a "bridge" between object-oriented programs
and relational databases or you can see the ORM as the layer that connects object
oriented programming (OOP) to relational databases.) frameworks such as Hibernate,
JPA(Java Persistence API), JDO(Java Data Objects), Oracle Toplink and iBATIS.
Java classes whose objects or instances will be stored in database tables are called
persistent classes in hibernate.
Spring with Hibernate
* Spring with JPA(Java Persistent API)
Product DAO impl implements Product DAO interface which uses hibernate
template which requires sessionfactory bean for which LocalSessionFactory
bean is a implementation
the application that this class is the source of spring beans,in this class we can define
multiple methods that returns beans of the different class,these method are marked