Spring
Spring
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.
1. class Employee{
2. Address address;
ADVANCED JAVA
3. Employee(){
4. address=new Address();
5. }
6. }
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.
Let's take the example of 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. Thus, it save a lot of JDBC code.
2) Loose Coupling
3) Easy to test
4) Lightweight
5) Fast Development
6) Powerful abstraction
7) Declarative support
Test
Context
Expression Language
Web
Here, we are going to learn the simple steps to create the first spring
application. To run this application, we are not using any IDE. We are
simply using the command prompt. Let’s see the simple steps to
create the spring application
Package com.javatpoint;
Return name;
System.out.println(“Hello: “+name);
<beans
Xmlns=http://www.springframework.org/schema/beans
Xmlns:xsi=http://www.w3.org/2001/XMLSchema-
instance
Xmlns:p=http://www.springframework.org/schema/p
Xsi:schemaLocation=”http://www.springframework.org/sche
ma/beans
ADVANCED JAVA
http://www.springframework.org/schema/beans/spring-
beans-3.0.xsd”>
</bean>
</beans>
The bean element is used to define the bean for the given
class. The property subelement of bean specifies the
property of the Student class named name. The value
specified in the property element will be set in the Student
class object by the IOC container.
Create the java class e.g. Test. Here we are getting the
object of Student class from the IOC container using the
getBean() method of BeanFactory. Let’s see the code of test
class.
Package com.javatpoint;
ADVANCED JAVA
Import org.springframework.beans.factory.BeanFactory;
Import
org.springframework.beans.factory.xml.XmlBeanFactory;
Import org.springframework.core.io.ClassPathResource;
Import org.springframework.core.io.Resource;
Resource resource=new
ClassPathResource(“applicationContext.xml”);
Student
student=(Student)factory.getBean(“studentbean”);
Student.displayInfo();
Now run the Test class. You will get the output Hello: Vimal
Jaiswal.
Com.springsource.org.apache.commons.logging-1.1.1
Org.springframework.beans-3.0.1.RELEASE-A
For the future use, You can download the required jar files
for spring core application.
Download the all jar files for spring including aop, mvc, j2ee,
remoting, oxm, etc.
To run this example, you need to load only spring core jar
files.
To load the jar files in eclipse IDE, Right click on your project
– Build Path – Add external archives – select all the required
jar files – finish..
Package com.javatpoint;
Return name;
This.name = name;
System.out.println(“Hello: “+name);
To create the xml file click on src – new – file – give the file
name such as applicationContext.xml – finish. Open the
applicationContext.xml file, and write the following code:
<beans
Xmlns=http://www.springframework.org/schema/beans
Xmlns:xsi=http://www.w3.org/2001/XMLSchema-
instance
ADVANCED JAVA
Xmlns:p=http://www.springframework.org/schema/p
Xsi:schemaLocation=”http://www.springframework.org/sche
ma/beans
http://www.springframework.org/schema/beans/spring-
beans-3.0.xsd”>
</bean>
</beans>
The bean element is used to define the bean for the given
class. The property subelement of bean specifies the
property of the Student class named name. The value
specified in the property element will be set in the Student
class object by the IOC container.
Create the java class e.g. Test. Here we are getting the
object of Student class from the IOC container using the
getBean() method of BeanFactory. Let’s see the code of test
class.
Package com.javatpoint;
Import org.springframework.beans.factory.BeanFactory;
Import
org.springframework.beans.factory.xml.XmlBeanFactory;
Import org.springframework.core.io.ClassPathResource;
ADVANCED JAVA
Import org.springframework.core.io.Resource;
Resource resource=new
ClassPathResource(“applicationContext.xml”);
Student
student=(Student)factory.getBean(“studentbean”);
Student.displayInfo();
Now run this class. You will get the output Hello: Vimal
Jaiswal.
Example: Let us say there is class GFG that uses SDI and
sets the property geeks. The code for it is given below.
Package com.geeksforgeeks.org;
Import com.geeksforgeeks.org.IGeek;
ADVANCED JAVA
Public class GFG {
IGeek geek;
{ This.geek = geek;
Package com.geeksforgeeks.org;
Import com.geeksforgeeks.org.IGeek;
IGeek geek;
GFG(IGeek geek)
{
ADVANCED JAVA
This.geek = geek;
Example of DI:
Public class Vehicle {
IEngine engine;
Tyres tyre;
Return tyre;
This.tyre = tyre;
}
ADVANCED JAVA
Public Vehicle(IEngine engine, Tyres tyre)
This.engine = engine;
This.tyre = tyre;
Public Vehicle() {}
Return engine;
This.engine = engine;
@Override
ApplicationContext rootctx
= new ClassPathXmlApplicationContext(
“springContext.xml”);
Vehicle obj1
= (Vehicle)rootctx
.getBean(“InjectwithConstructor”);
Vehicle obj2
= (Vehicle)rootctx
.getBean(“InjectwithSetter”);
System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj1 == obj2);
}
ADVANCED JAVA
Types of Advices:
We need to write a lot of code before and after executing the query, such as creating
connection, statement, closing resultset, connection etc.
Repetition of all these codes from one to another database logic is a timeconsuming task.
Spring JdbcTemplate eliminates all the above mentioned problems of JDBC API. It provides
you methods to write the queries directly, so it saves a lot of work and time.
JdbcTemplate
NamedParameterJdbcTemplate
No. Method Description
1) public int update(String is used to insert,
query) update and delete
ADVANCED JAVA records.
2) public int update(String is used to insert,
query,Object… args) update and delete
SimpleJdbcTem
records using
plate
PreparedStatement
using given
SimpleJdbcInser
arguments.
3) public void execute(String is used to execute t and
query) DDL query. SimpleJdbcCall
4) public T execute(String sql, executes the query
PreparedStatementCallback by using JdbcTemplate
action) PreparedStatement class
It is the central
class in the
Spring JDBC
support classes.
It takes care of
creation and
release of resources such as creating and closing of connection object etc. So it will not
lead to any problem if you forget to close the connection.
It handles the exception and provides the informative exception messages by the help of
excepion classes defined in the org.springframework.dao package.
We can perform all the database operations by the help of JdbcTemplate class such as
insertion, updation, deletion and retrieval of the data from the database.
There are a lot of advantage of Spring framework in respect to ORM frameworks. There are
as follows:
Less coding is required: By the help of Spring framework, you don’t need to write extra
codes before and after the actual database logic such as getting the connection, starting
transaction, commiting transaction, closing connection etc.
Easy to test: Spring’s IoC approach makes it easy to test the application.
Better exception handling: Spring framework provides its own API for exception handling
with ORM framework.
Integrated transaction management: By the help of Spring framework, we can wrap our
mapping code with an explicit template wrapper class or AOP style method interceptor.