Spring Core
Spring Core
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 :
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.