Spring Tutorials

Spring Tutorial

Introduction of Spring Framework

Advantages and Disadvantages of Spring Framework

Features of Spring Framework

Basic concept of Spring Framework

Architecture of Spring Framework

Modules of Spring Framework

Goal of Spring Framework

Create Application of Spring without any IDE

Create Application of Spring in Eclipse IDE

Create Application of Spring in MYEclipse IDE

Important JAR of Spring Framework

IOC container in Spring Framework

Bean and Applicationcontext container in Spring Framework

Example of Spring BeanFactory Container in Spring Framework

Example of Spring ApplicationContext Container in Spring Framework

Bean in Spring Framework

Lifecycle of Bean in Spring Framework

Scope of Bean in Spring Framework

Autowiring in Spring Framework

Dependency Injection in Spring Framework

Constructor-based Dependency Injection Example in Spring Framework

Setter-based Dependency Injection Example in Spring Framework

Introduction of AOP in Spring Framework

Core concept and Goal of AOP in Spring Framework

AOP Proxies in Spring framework

XML Schema Based AOP in Spring Framework

AOP Xml based Configuration Example in Spring Framework

Example of Declaring AOP Advices in Spring Framework

AOP AspectJ Annotation with Example in Spring Framework

Declaring an aspect and pointcut using Annotation in Spring Framework

Declaring AOP Advices using Annotation Example in Spring Framework

DAO support in Spring Framework

Introduction of Spring JDBC Framework in Spring Framework

Introduction of Spring JdbcTemplate in Spring Framework

Example of Spring JdbcTemplate class in Spring framework

Example of Executing SQL statements in Spring Framework

Example of Executing DDL statements in Spring Framework

SQL Stored Procedure in Spring Framework

Example of NamedParameterJdbcTemplate in Spring Framework

Example of RowMapperJdbcTemplate in Spring Framework

Introduction of ORM Framework in Spring Framework

Integration of Spring with Hibernate in Spring Framework

Integration of Spring with JPA in Spring Framework

Introduction of Spring Expression Language (SpEL) in Spring Framework

Example of Spring Expression Language (SpEL) in Spring framework

Example of Spring EL in XML and Annotation in Spring Framework

Language Reference with SpEL in Spring Framework

Operators in Spring Expression Language(SpEL) in Spring Framework

Variable in Spring Expression Language(SpEL) in Spring Framework

Introduction of Spring Framework Transaction Management in Spring Framework

Spring Framework Transaction Abstraction

Spring Declarative Transaction Management in Spring Framework

Spring Programmatic Transaction Management in Spring Framework

Introduction of Spring OXM (Object XML Mapping) in Spring Framework

Integration of Spring with Jaxb Example in Spring framework

Example of Spring with Xstream in Spring Framework

Example of Spring with Castor in Spring Framework

Remote Method Invocation(RMI) in Spring Framework

Spring and RMI Integration with Example in Spring Framework

Example of Spring and Httpinvoker in Spring Framework

Example of Spring and Hessian in Spring Framework

Integration of Spring with JMS in Spring Framework

Introduction of Webservice in Spring Framework

Spring Web Services in Spring Framework

Web Services with Jax-WS in Spring framework

Exposing and Exporting servlet-based Web Services using JAX-WS in Spring Framework

Accessing Web Services using JAX-WS in Spring Framework

Introduction of JMS in Spring Framework

JMS Messaging Models in Spring Framework

Using Spring JMS in Spring Framework

Sending and Receiving a Message Using JMS API in Spring Framework

Introduction of JMX (Java Management Extension) in Spring Framework

Integrating Beans with JMX in Spring Framework

Creating a MBeanServer in Spring Framework

Introduction Java Mail with Spring in Spring Framework

Example of Java Mail with spring in Spring Framework

Introduction of EJB(Enterprise JavaBeans) in Spring Framework

Introduction of EJB(Enterprise JavaBeans) Integration in Spring Framework

Integration of Spring With Struts 2 Example in Spring Framework

Spring MVC

Spring MVC

adplus-dvertising
Spring Declarative Transaction Management
Previous Home Next

The Spring Framework's declarative transaction management is made possible with Spring aspect-oriented programming (AOP), although, as the transactional aspects code comes with the Spring Framework distribution and may be used in a boilerplate fashion, AOP concepts do not generally have to be understood to make effective use of this code.

Declarative transaction is a spring transaction strategy. The other strategies are programmatic transaction and local transaction. There are two ways to accomplish declarative transaction, through xml or through annotations. In this tutorial we look at xml based declarative transaction.

The advantage of a declarative transaction is that all transaction demarcation happens through the xml and no extra coding needs to be done. It is also possible to declarative modify the transaction attributes without touching the code. Also changing the transaction managers is only a matter of modifying the transactionmanager bean.

Spring Declarative Transaction Management Example

First you make a table in Database Mysql Workbench like employee. The code of table as given below:

CREATE TABLE `employee` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  `salary` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) 
Technologies used to run this Application.
  1. Spring 3.0
  2. Eclipse IDE
  3. JDK 1.6
  4. MySql Workbench 6.2

Let us have working Eclipse IDE in place and follow the following steps to create a Spring application:

Step1. Now you create a simple Java Project using Eclipse IDE. Follow the option File -> New -> Project and finally select Java Project wizard from the wizard list. Now name your project as SpringDeclarativeTransaction using the wizard window as given below.

Add spring jar files

Step2. Now add spring jar in your project, To do this, right click on your project name SpringDeclarativeTransaction and then follow the following option available in context menu: Build Path -> Configure Build Path to display the Java Build Path window as given below. There are some jar files required to run this application, 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.

  1. antlr-runtime-3.0.1
  2. org.springframework.aop-3.1.0
  3. org.springframework.asm-3.1.0.
  4. org.springframework.aspects-3.1.0.
  5. org.springframework.beans-3.1.0.
  6. org.springframework.context.support-3.1.0.
  7. org.springframework.context-3.1.0.
  8. org.springframework.core-3.1.0.
  9. spring-expression-3.1.0.RELEASE.jar
  10. com.springsource.org.aopalliance-1.0.0.jar
  11. org.apache.commons.logging-1.1.1.jar
  12. org.springframework.jdbc-3.0.0.M2.jar
  13. org.springframework.transaction-3.1.0.RELEASE.jar
  14. mysql-connector-java-5.1.23-bin.jar
  15. commons-dbcp-1.4.jar
  16. ojdbc14.jar
  17. aspectjweaver-1.6.8.jar
  18. commons-pool-1.4.jar
  19. hibernate-core.jar
  20. spring-hibernate3-2.0.5.jar
Create Java class file

Step3. Now you create simply java file with any name. First we need to create a package with any name like com.r4r. To do this, Right click on src in package explorer section and follow the option : New -> Package. and then Right click on package(com.r4r) - New - class - Write the class name e.g. Employee - finish. Write the following code:

package com.r4r;
public class Employee {
	private int id;  
	private String name;  
	private int salary;

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
}

Step4. Now you create another Java class named EmployeeDao.java.

package com.r4r;
import java.util.List;
import javax.sql.DataSource;

public interface EmployeeDao {
   public void setDataSource(DataSource ds);

 //Insert Method to save data in Database.
 public void create(Integer id, String name,  Integer salary);

  //Delete data into the Database.
   void delete(Integer id); 

   //Show all data in Database table.
   public List<Employee> listEmployees();
}

Step5. Now you create another Java class named EmployeeMapper.java.

package com.r4r;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;

public class EmployeeMapper implements RowMapper<Employee> {
public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {

	   Employee employee = new Employee();
	   employee.setId(rs.getInt("id"));
	   employee.setName(rs.getString("name"));
	   employee.setSalary(rs.getInt("salary"));
       return employee;
   }
}

Step6. Now you create another Java class named EmployeeJdbcTemplate.java.

package com.r4r;

import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class EmployeeJdbcTemplate implements EmployeeDao{
   private JdbcTemplate jdbcTemplateObject;

   public void setDataSource(DataSource dataSource) {
      this.jdbcTemplateObject = new JdbcTemplate(dataSource);
   }

public void create(Integer id, String name, Integer salary){
String SQL1 = "insert into employee (id, name, salary) values (?, ?, ?)";
jdbcTemplateObject.update( SQL1, id, name, salary);
System.out.println
 ("Created Id = "+ id + ", Name = " + name + ", Salary = " + salary);}

   public void delete(Integer id) {  
	   String SQL = "DELETE FROM employee WHERE id = ?";  
	   jdbcTemplateObject.update(SQL, new Object[]{id});  
	   System.out.println("Deleted Record with ID = " + id );  
	  } 

 public List<Employee> listEmployees() {
  String SQL = "select * from employee ";
  List <Employee> employee=jdbcTemplateObject.query(SQL, new EmployeeMapper());
      return employee;
   }
}

Step7. Now create the xml file click on src- new -file -give the file name like applicationContext.xml then finish.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

   <!-- Initialization for data source -->
   <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/amit"/>
      <property name="username" value="root"/>
      <property name="password" value=""/>
   </bean>
  
   <tx:advice id="txAdvice"  transaction-manager="transactionManager">
      <tx:attributes>
      <tx:method name="create"/>
      </tx:attributes>
   </tx:advice>
	
   <aop:config>
      <aop:pointcut id="createOperation" 
      expression="execution(* com.r4r.EmployeeJdbcTemplate.create(..))"/>
      <aop:advisor advice-ref="txAdvice" pointcut-ref="createOperation"/>
   </aop:config>
	
   <!-- Initialization for TransactionManager -->
   <bean id="transactionManager"
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource"  ref="dataSource" />    
   </bean>

   <!-- Definition for employeeJDBCTemplate bean -->
   <bean id="employeeJdbcTemplate"  
   class="com.r4r.EmployeeJdbcTemplate">
      <property name="dataSource"  ref="dataSource" />  
   </bean>

</beans>

Step8. Now Create the java Main class (Test.java).

package com.r4r;

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
   public static void main(String[] args) {
      ApplicationContext context = 
           new ClassPathXmlApplicationContext("applicationContext.xml");

      EmployeeDao employeeJdbcTemplate = 
      (EmployeeDao)context.getBean("employeeJdbcTemplate");
      
      System.out.println("------Records creation--------" );
      employeeJdbcTemplate.create(100, "John" , 7000);
      employeeJdbcTemplate.create(101, "Peter" , 8000);  
      
      System.out.println("------Records Deletion--------" );
      employeeJdbcTemplate.delete(114);
      employeeJdbcTemplate.delete(119);
    
      System.out.println("------Listing all the records--------" );
      List<Employee> employee = employeeJdbcTemplate.listEmployees();
      for (Employee record : employee) {
         System.out.print("ID : " + record.getId() );
         System.out.print(", Name : " + record.getName() );
         System.out.print(", Salary : " + record.getSalary());
       
      }
   }
}
Run Application

Now run the Application you see the following output:

And this is the Database Output of the above Program.

Previous Home Next