Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Spring Basics

Framework:
A framework is a collection of components that can used to build well organized applications.

Spring Framework:
Spring is a framework, because it provides components to build common parts of applications, such as data access support, MVC support, and many others.

The core of the Spring Framework is based on the principle of Inversion of Control (IoC). Applications that follow the IoC principle use configuration that describes the dependencies between its components. It is then up to the IoC framework to satisfy the configured dependencies. The “inversion” means that the application does not control its structure; it is up to the IoC framework to do that.

IoC:
IoC provides services through which a component can access its dependencies and services for interacting with the dependencies throughout their lives. In general, IoC can be decomposed into two subtypes: DI and dependency lookup

DI:
Dependency Injection (DI) in computer programming refers to the process of supplying an external dependency to a software component. It is a specific form of inversion of control where the concern being inverted is the process of obtaining the needed dependency.
Three types
1. interface injection
2. setter injection
3. constructor injection

Dependency Lookup:
It’s the reverse process of DI where a component must acquire a reference to a dependency, whereas with DI, the dependencies are literally injected into the component by the IoC container
two types are
1. Dependency pull
2. Contextualized dependency lookup (CDL

AOP:
Aspect-oriented programming (AOP) is one of the technologies of the moment in the programming space. AOP lets us to implement crosscutting logic—that is, logic that applies to many parts of your application—in a single place, and then have that logic automatically applied right across the application

there are two types of AOP

1. Static AOP, such as AspectJ.
2. Dynamic AOP, such as that in Spring, allows crosscutting logic to be applied arbitrarily to any other code at runtime


Hibernate Annotations and Spring

Let us make the previous Hibernate Application to Spring application with the same entity class Item.java and Sale.java

Step 1:

Let us make DAO classes MyDAO.java and MyDAOImpl.java

//MyDAO.java
package me.gs.dao;
import me.gs.model.Item;

import me.gs.model.Sale;

public interface MyDAO
{
public void saveItem(Item item);

public void saveSale(Sale sale);

}


//MyDAOImpl.java

package me.gs.dao;

import me.gs.model.Item;

import me.gs.model.Sale;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

@Repository("MyDAO")
public class MyDAOImpl implements MyDAO
{

private HibernateTemplate hibernateTemplate;


@Autowired

public void createHibernateTemplate(SessionFactory sessionFactory)
{

this.hibernateTemplate = new HibernateTemplate(sessionFactory);

}


public void saveItem(Item item)
{

hibernateTemplate.save(item);
}

public void saveSale(Sale sale)
{

hibernateTemplate.save(sale);
}

}


Step 2:

Add jdbc.properties file inside the WEB-INF directory with bellow code

jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/my jdbc.username=root jdbc.password=

Step 3:
we may remove bellow code from hibernate.cfg.xml or comment or let it be exist in fact its not needed as we added jdbc.properties file

< property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect < /property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/my</property> <property name="hibernate.connection.username">root</property>

Step 4:
Locate the applicationContext.xml and edit with the bellow code inside the WEB-INF folder




Step 5:
Let us make a jUnit test class to test the application this time.

import me.gs.dao.MyDAO;

import me.gs.model.Item;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TestSpring
{

public TestSpring()
{

}

@Test

public void testSpringHB()
{

ApplicationContext context = new FileSystemXmlApplicationContext("web/WEB-INF/applicationContext.xml");


MyDAO idao = (MyDAO) context.getBean("MyDAO");
Item item = new Item();

item.setName("item3");

item.setCode("code3");

idao.saveItem(item);
}

}
Output comes as bellow: