W.T. Unit-Iv

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Spring

It was developed by Rod Johnson in 2003. Spring framework makes


the easy development of JavaEE application.

It is helpful for beginners and experienced persons.

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. We will learn these modules in next page.
Let's understand the IOC and Dependency Injection first.

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.
Let's understand this with the following code:

1. class Employee{
2. Address address;
3. Employee(){
4. address=new Address();
5. }
6. }

In such case, there is dependency between the Employee and Address


(tight coupling). In the Inversion of Control scenario, we do this something
like this:

1. class Employee{
2. Address address;
3. Employee(Address address){
4. this.address=address;
5. }
6. }
Thus, 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.

Advantages of Spring Framework


There are many advantages of Spring Framework. They are as follows:

1) 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.

2) Loose Coupling

The Spring applications are loosely coupled because of dependency


injection.

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) 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.

5) Fast Development

The Dependency Injection feature of Spring Framework and it support to


various frameworks makes the easy development of JavaEE application.

6) Powerful abstraction

It provides powerful abstraction to JavaEE specifications such


as JMS, JDBC, JPA and JTA.

7) Declarative support

It provides declarative support for caching, validation, transactions and


formatting.
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. Dependency Injection makes our programming
code loosely coupled. To understand the DI better, Let's understand the
Dependency Lookup (DL) first:

Dependency Lookup
The Dependency Lookup is an approach where we get the resource after
demand. There can be various ways to get the resource for example:

1. A obj = new AImpl();

In such way, we get the resource(instance of A class) directly by new


keyword. Another way is factory method:

1. A obj = A.getA();

Alternatively, we can get the resource by JNDI (Java Naming Directory


Interface) as:

1. Context ctx = new InitialContext();


2. Context environmentCtx = (Context) ctx.lookup("java:comp/env");
3. A obj = (A)environmentCtx.lookup("A");

There can be various ways to get the resource to obtain the resource.
Let's see the problem in this approach.

Problems of Dependency Lookup

There are mainly two problems of dependency lookup.

o tight coupling The dependency lookup approach makes the code


tightly coupled. If resource is changed, we need to perform a lot of
modification in the code.
o Not easy for testing This approach creates a lot of problems while
testing the application especially in black box testing.

Dependency Injection
The Dependency Injection is a design pattern that removes the
dependency of the programs. In such case we provide the information
from the external source such as XML file. It makes our code loosely
coupled and easier for testing. In such case we write the code as:

1. class Employee{
2. Address address;
3.
4. Employee(Address address){
5. this.address=address;
6. }
7. public void setAddress(Address address){
8. this.address=address;
9. }
10.
11. }

In such case, instance of Address class is provided by external souce such


as XML file either by constructor or setter method.

Two ways to perform Dependency Injection in Spring


framework
Spring framework provides two ways to inject dependency

o By Constructor
o By Setter method

Dependency Injection by Constructor


Example
We can inject the dependency by constructor. The <constructor-
arg> subelement of <bean> is used for constructor injection. Here we
are going to inject

1. primitive and String-based values


2. Dependent object (contained object)
3. Collection values etc.

Injecting primitive and string-based values


Let's see the simple example to inject primitive and string-based values.
We have created three files here:
o Employee.java
o applicationContext.xml
o Test.java

Employee.java

It is a simple class containing two fields id and name. There are four
constructors and one method in this class.

1. package com.javatpoint;
2.
3. public class Employee {
4. private int id;
5. private String name;
6.
7. public Employee() {System.out.println("def cons");}
8.
9. public Employee(int id) {this.id = id;}
10.
11. public Employee(String name) { this.name = name;}
12.
13. public Employee(int id, String name) {
14. this.id = id;
15. this.name = name;
16. }
17.
18. void show(){
19. System.out.println(id+" "+name);
20. }
21.
22. }
applicationContext.xml

We are providing the information into the bean by this file. The
constructor-arg element invokes the constructor. In such case,
parameterized constructor of int type will be invoked. The value attribute
of constructor-arg element will assign the specified value. The type
attribute specifies that int parameter constructor will be invoked.

1. <?xml version="1.0" encoding="UTF-8"?>


2. <beans
3. xmlns="http://www.springframework.org/schema/beans"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xmlns:p="http://www.springframework.org/schema/p"
6. xsi:schemaLocation="http://www.springframework.org/schema/
beans
7. http://www.springframework.org/schema/beans/spring-
beans-3.0.xsd">
8.
9. <bean id="e" class="com.javatpoint.Employee">
10. <constructor-arg value="10" type="int"></constructor-arg>
11. </bean>
12.
13. </beans>
Test.java

This class gets the bean from the applicationContext.xml file and calls the
show method.

1. package com.javatpoint;
2.
3. import org.springframework.beans.factory.BeanFactory;
4. import org.springframework.beans.factory.xml.XmlBeanFactory;
5. import org.springframework.core.io.*;
6.
7. public class Test {
8. public static void main(String[] args) {
9.
10. Resource r=new ClassPathResource("applicationContext.
xml");
11. BeanFactory factory=new XmlBeanFactory(r);
12.
13. Employee s=(Employee)factory.getBean("e");
14. s.show();
15.
16. }
17. }
Output:10 null

Injecting string-based values


If you don't specify the type attribute in the constructor-arg element, by default string type
constructor will be invoked.

1. ....
2. <bean id="e" class="com.javatpoint.Employee">
3. <constructor-arg value="10"></constructor-arg>
4. </bean>
5. ....

If you change the bean element as given above, string parameter constructor will be invoked
and the output will be 0 10.

Output:0 10

Design Patterns in the Spring Framework

Design patterns are an essential part of software development.


These solutions not only solve recurring problems but also help
developers understand the design of a framework by recognizing
common patterns.
the most common design patterns used in the Spring Framework:

1. Singleton pattern
2. Factory Method pattern
3. Proxy pattern
4. Template pattern

Singleton Pattern
The singleton pattern is a mechanism that ensures only one
instance of an object exists per application. This pattern can be
useful when managing shared resources or providing
cross-cutting services, such as logging.

You might also like