Pages

Friday 26 April 2013

Spring AOP

Spring Aspect Oriented Programming is powerful feature of spring

Each application will have some global function/feature to do which applies for all/many/some of the objects depending on the requirement.

To achive such global function/feature spring AOP provides very powerful and effective way.

Basic Terms in AOP

1)Aspect - cross cutting concern ,something global feature of the application.

2)Join Point - point during the execution of the program such as method execution or exception handling code. in simple words the point where the cross cutting concerns can be applied

3)Advice - the Action taken at the join point. means implementation of any global feature/cross cutting feature.

4)Point cut - Expression which matches the exact join points.


Example program

below is the registration program which has the logic for registration.






below is the login program which has the login logic.

 

Now assume i want to have audit details where i want to store each action like register/login etc in some table.
Means basically , iwant to fire insert query to DB on execution of each of the above program.

so what could be done

without AOP, we can call insert query in each of the programs above.
but what if there are 100 such different actions/programs in our applications ?
we need to write insert query in 100 places  or atleast we have to write 100 calls to that insert query


Now we will see how it can be achived very effectively using AOP.

Include following jars in the classpath
1)aspectjrt.jar
2)aspectjweaver.jar
3)cgilib.jar
4)aopalliance.jar
5)asm.jar

add following tag in spring configuration file ie beans.xml in our case
<aop:aspectj-autoproxy/> as follows



create a new service class to add the dependencies and name it has ApplicationService and is as below




Now write our Aspect as follows

 

now see because of the pointcut expression matched to any method inside com.kb package, our audit() method having insert query logic executes automatically without any of the call to this audit() method.

No matter how many methods will be , we can configure using pointcut and wildcard expression and we can make the call automatically without writing in any code in any of the method.

Now run the main program as below


output will be as follows






 Observe that after user registration audit is called and after login also audit aspect is called automatically.

Thanks for Reading.



Wednesday 3 April 2013

Spring - Dependency Injection

Dependency Injection : as the term itself indicates it is injecting dependencies.
In terms of java simple words - providing state full dependent java object to the object which has dependency.
ex:
Class A{
B b;
}
here, providing object b with full state to object of class A.
Lets see in detail
Observe the below example - computer object has dependency on computerDetails object
package com.kb.spring.two.five;
import java.util.Date;
public class Computer {
private String productNo;
private double cost;
private Date purchaseDate;
private ComputerDetails computerDetails;
public Computer() {
System.out.println("computer default constructor");
}
public String getProductNo() {
return productNo;
}
public void setProductNo(String productNo) {
this.productNo = productNo;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public Date getPurchaseDate() {
return purchaseDate;
}
public void setPurchaseDate(Date purchaseDate) {
this.purchaseDate = purchaseDate;
}
public ComputerDetails getComputerDetails() {
return computerDetails;
}
public void setComputerDetails(ComputerDetails computerDetails) {
this.computerDetails = computerDetails;
}
}
now look at computerDetails class
package com.kb.spring.two.five;
public class ComputerDetails {
private int ramSize;
private int hardDiskSize;
private double processorSpeed;
public ComputerDetails() {
System.out.println("computerDetails default constructor");
}
public int getRamSize() {
return ramSize;
}
public void setRamSize(int ramSize) {
this.ramSize = ramSize;
}
public int getHardDiskSize() {
return hardDiskSize;
}
public void setHardDiskSize(int hardDiskSize) {
this.hardDiskSize = hardDiskSize;
}
public double getProcessorSpeed() {
return processorSpeed;
}
public void setProcessorSpeed(double processorSpeed) {
this.processorSpeed = processorSpeed;
}
}
Now look at the client program without using Spring Dependency Injection
package com.kb.spring.two.five;
import java.util.Date;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
public class ClientWithoutSpring {
public static void main(String[] args) {
Computer computer = new Computer();
computer.setCost(37500.00);
computer.setProductNo("123");
computer.setPurchaseDate(new Date());
ComputerDetails computerDetails = new ComputerDetails();
computerDetails.setRamSize(2);
computerDetails.setHardDiskSize(500);
computerDetails.setProcessorSpeed(3.2);
computer.setComputerDetails(computerDetails);
System.out.println("computer details are as below");
System.out.println("---------------------------");
System.out.println("prod No "+computer.getProductNo());
System.out.println("cost is "+computer.getCost());
System.out.println("purchase date is "+computer.getPurchaseDate());
System.out.println("computer parts details are as below");
System.out.println("------------------------------------");
System.out.println("Ram "+computer.getComputerDetails().getRamSize());
System.out.println("hard disk "+computer.getComputerDetails().getHardDiskSize());
System.out.println("processor speed "+computer.getComputerDetails().getProcessorSpeed());
System.out.println("-----end-------------");
}
}
Observation : if you look at the above class,it is creating ComputerDetails object(its dependency) by itself...this is normal java way of obtaining dependent objects.
Now look at the client program using spring dependency
package com.kb.spring.two.five;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ClientWithApplicationContext {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/kb/two/five/beans.xml");
Computer computer = context.getBean("computer",Computer.class);
System.out.println("computer details are as below");
System.out.println("---------------------------");
System.out.println("prod No "+computer.getProductNo());
System.out.println("cost is "+computer.getCost());
System.out.println("purchase date is "+computer.getPurchaseDate());
System.out.println("computer parts details are as below");
System.out.println("------------------------------------");
System.out.println("Ram "+computer.getComputerDetails().getRamSize());
System.out.println("hard disk "+computer.getComputerDetails().getHardDiskSize());
System.out.println("processor speed "+computer.getComputerDetails().getProcessorSpeed());
System.out.println("-----end-------------");
}
}
and beans.xml file to configure the beans
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5..xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="computer">
<property name= "productNo" value="123"></property>
<property name= "cost" value="37500.00"></property>
<property name= "purchaseDate" value="18/03/2013"></property>
<property name= "computerDetails" ref="computerDetails"></property>
</bean>
<bean id="computerDetails">
<property name="ramSize" value="2"></property>
<property name="hardDiskSize" value="500"></property>
<property name="processorSpeed" value="3.2"></property>
</bean>
</beans>
in beans.xml, we have added the beans definition, and we specified that Computer is dependent on ComputerDetails by using ref key word.
thas it, in the client program above , when we are getting Computer bean using context.getBean(), its dependent object ComputerDetails is created and initialized by the container and it injects it into the Computer object. this concept is called dependency injection in Spring.
Why it is called IOC ?
If the object having dependency is creating the dependent object then it is a straight way.
if someone else is creating your dependency then control is with that 3rd person(spring) hence it is called Inversion of control.