Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


Aspect Oriented Programming Technologies

AOP (aspect oriented programming) is a programming methodology that enable the modularization of concerns that cut across multiple types and objects.
AOP Terminologies:
Aspect: a modularization of a concern that cuts across multiple classes.
Join point: a point during the execution of a program, such as the execution of a method.
Advice: action taken by an aspect at a particular join point. Different types of advice include "around," "before", "after" advice etc.
Pointcut: a predicate that matches join points.
Spring AOP supports 5 types of advices:
Before advice: Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).
After returning advice: Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception.
After throwing advice: Advice to be executed if a method exits by throwing an exception.
After (finally) advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).
Around advice: Advice that surrounds a join point such as a method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.
Now we are taking a scenario where there is a small class and how different advices can be apply in different situations:
@Before advice - Will be used for authentication of guests. Only people in guest list can come.
@Around advice - Will be used for Audit. Guest's entry and exit timings should be noted.
@AfterThrowing - Will be used for Exception handling. If a guest gets too drunk then call a cab.
@AfterReturning - Will be used to Check Normal flow. Give good bye gift to guests who didn't get too drunk.
@After - Will be used to handle both normal or Exception conditions. Send a thank you email to all the guest irrespective of whether they were drunk or not.
Now we have to add below jar in projects.
aspectjrt.jar
aspectjweaver.jar
aopalliance.jar
cglib-2.2.2.jar
asm-3.3.1.jar

First we create a simple example, then we will apply aspect oriented programming

Rose.java

package org.vtechnology.model;

public class Rose {

private String name;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Lotus.java

package org.vtechnology.model;

public class Lotus {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

Flower.java

package org.vtechnology.service;

import org.vtechnology.model.Lotus;
import org.vtechnology.model.Rose;

public class Flower {

private Rose rose;
private Lotus lotus;
public Rose getRose() {
return rose;
}
public void setRose(Rose rose) {
this.rose = rose;
}
public Lotus getLotus() {
return lotus;
}
public void setLotus(Lotus lotus) {
this.lotus = lotus;
}
}

spring.xml

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context">

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<aop:aspectj-autoproxy/>

<bean id="rose" class="org.vtechnology.model.Rose">
<property name="name" value="Flower Name is Rose"/>
</bean>

<bean id="lotus" class="org.vtechnology.model.Lotus">
<property name="name" value="Flower Name is Lotus"/>
</bean>

<bean name="flower" class="org.vtechnology.service.Flower"
autowire="byName">

</beans>

AopMain.java

package org.vtechnology.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AopMain {

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

Flower flower = context.getBean("flower", Flower.class);
System.out.println(flower.getRose().getName());
System.out.println(flower.getLotus().getName());
}
}

This is simple example using dependency injection. Now We will add AOP functionality on this program. We will create sepeate class LoggingAOP for using AOP functionality. And add <aop:aspectj-autoproxy/> in spring.xml but in this example we have already added aop:aspectj-autoproxy in spring.xml file

LoggingAOP.java

package org.vtechnology.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspeLogging Advice Run beforectj.lang.annotation.Before;

@Aspect
public class LoggingAOP {

@Before("execution(public String getName())")
public void loggingAdvice(){
System.out.println("Logging Advice Run before getName Method");
}

}

Add org.vtechnology.aop.LoggingAOP class entry in spring.xml file.

spring.xml

.......
.......
<bean class="org.vtechnology.aop.LoggingAOP"/>

</beans>

Now if you run this example, you will see "Logging Advice Run before getName Method" message display before run getname method. I am


     << Previous
Next >>