Struts1 Spring and Hibernate Integration

Register.jsp

Failure.jsp

Success.jsp

web.xml

struts-config.xml

applicationContext.xml

RegisterForm.java

RegisterAction.java

Register.hbm.xml

Register.java

RegisterDao.java

RegisterDao.Impl.java

RegisterBoImpl.java

RegisterBo.java

Spring web application

This is a complete web application of spring jdbc module of spring framework

First we write presentation logic

index.html

list.html

newaccount.html

delete.html

transaction.html

web.xml

applicationContext.xml

Templet.java

dsinter.java

insertsevlet.java

deleteservlet.java

selectservlet.java

showservlet.java

updateservlet.java

Spring MVC Basics

Spring mvc module provide the complete mvc architecture pattern implementation by only spring framework presentation layer implemented through jsp. Front controller is written in servlet and business logic is provided through special controller. In spring 2.5 provides spring mvc and spring web module are combined there is no need separate jar files. In this module has a capability to integrate other web framework, ie struts,jsf etc.

Spring DAO

This module provide the abstraction layer of spring related application

Dao Support classes are :

  1. JdbcDaoSupport – Super class for JDBC data access objects. Requires a DataSource to be set, providing a JdbcTemplate based on it to sub classes.
  2. HibernateDaoSupport – Super class for Hibernate data access objects. Requires a SessionFactory to be set,providing a HibernateTemplate based on it to sub classes. Can alternatively be initialized directly via a HibernateTemplate, to reuse the latter’s settings like SessionFactory, flush mode, exception translator, etc.
  3. JdoDaoSupport – Super class for JDO data access objects. Requires a persistenceManagerFactory to be set,providing a JdoTemplate based on it to sub classes.

Spring Transaction Management

Provides a consistent abstraction for transaction management. This abstraction is one of the most important of Spring’s abstractions wethout transaction data definition language operation is not perform, and delivers the following benefits:

1 Provides a consistent programming model across different ORM Database Layer and JDO.
2 Provides a simpler, easier to use, API for programmatic transaction transaction APIs
3 Integrates with the Spring data access abstraction
4 Supports Spring declarative transaction management

Transaction strategies

The key to the Spring transaction abstraction is the notion of a transaction strategy.
This is in org.springframework.transaction.PlatformTransactionManager interface Methods are available in transaction are

1 beginTransaction().
2 commitTransaction().
3 rolleBack().
4 setTimeout().

And these methods show TransactionException

every transaction follows ACID property

1 A atomicity.
2 C compatibility.
3 I isolation.
4 D dueribility.

We are using hibernate supported transaction management we put following code in beans in spring

The default transactions strategies are apply are as follows:

• Exception Handling: RuntimeExceptions roll-back, normal (checked) Exceptions don’t
• Transactions are read/write
• Isolation Level: TransactionDefinition.ISOLATION_DEFAULT
• Timeout: TransactionDefinition.TIMEOUT_DEFAULT

Spring Aspect Oriented Programming

AOP is a Aspect Oriented Programming not a object oriented programming this is a methodology to provide other middle ware services such as security transaction management etc.
Spring AOP is implemented in pure Java. There is no need for a special compilation process. Spring AOP does not need to control the class loader hierarchy, and is thus suitable for use in a J2EE web container or application server.

Spring AOP provides other methodology such as join,join points,point cuts,advice.
Different advice types include are

1 Around advice
2 Before advice
3 Throws advice
4 After returning advice

Spring Bean Example

This application is the example of been collaboration that means one bean injecting the information of other beans
Book.java

This is normal pojo class

BookInter.java

this is implementation details class file

CommandLineView.java

this class displaying the out put.

book.xml

This is client application of all application

Output

Spring configuration first example

Resource of spring core module Applications are used
1 Spring interface.
2 Spring bean class.
3 Spring- configuration file.
4 Client Applications
All the spring releted jar files put in lib folder directory structure is same as normal java/j2ee applications.

Demo.xml

This example is setter base dependency injection this xml file is spring configuration file we set all those bens in this xml file. Again we create interface and its implantation class.

DemoInterImpl.java

DemoInter.java

DemoClient.java

When we compile and run this program this will print massage with username and current date and time.

Inversion of Control/Dependency Injection

The basic principle is that beans define their dependencies (i.e. the other objects they work with) only through constructor arguments, arguments to a factory method, or properties which are set on the object instance after it has been constructed or returned from a factory method

Dependency Injection are of two types

1 Constructor based dependency injection.
2 Setter based dependency injection

setter-based dependency injection is realized by calling setters on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean. Beans defined in the BeanFactory that use setter-based dependency injection are true JavaBeans

constructor-based dependency injection is realized by invoking a constructor with a number of arguments,each representing a collaborator or property. Additionally, calling a static factory method with specific arguments, to construct the bean.

BeanFactory and Bean Definitions

The BeanFactory and ApplicationContext is the actual container which instantiates, configures, and manages a number of beans.
A BeanFactory comes by the interface org.springframework.beans.factory.BeanFactory, for which
there are multiple implementations

ApplicationContext comes in spring j2ee modules i.e spring 2.5

Activating spring beanFactory container is not but creating object for class that implements “org.springframework.beans.factory.BeanFactory” interface.

ApplicationContexts are a subclass of BeanFactory, and most users end up using XML variants of
ApplicationContext).

There are three way to instantiate beanFactory

Or

Or