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 >>     


Advice

Advices are actions taken for a particular join point. In terms of programming, they are methods that gets executed when a certain join point with matching pointcut is reached in the application.

Types of advice:

There are following types of advices support in Aspect Oriented Programming (AOP)

  • 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. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the 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.

Around advice is the most general kind of advice. Since Spring AOP, like AspectJ, provides a full range of advice types, we recommend that you use the least powerful advice type that can implement the required behavior. For example, if you need only to update a cache with the return value of a method, you are better off implementing an after returning advice than an around advice, although an around advice can accomplish the same thing. Using the most specific advice type provides a simpler programming model with less potential for errors. For example, you do not need to invoke the proceed() method on the JoinPoint used for around advice, and hence cannot fail to invoke it.

Example of After Advice

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) {
System.out.println("Setter Method from Lotus Called");
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">

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

</beans>

LoggingAOP.java

package org.vtechnology.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.After;

@Aspect
public class LoggingAOP {

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

@Pointcut("execution(public String get*())")
public void pointcutGetter() {
}

@Before("allGetter1()")
public void loggingAdvice1(JoinPoint joinPoint){
}

@Pointcut("within(org.vtechnology.model.Lotus)")
public void allGetter1(){
}

@After("args(name)")
public void stringArgumentMethod(String name){
System.out.println("A Method that takes String Arguments has been
called. The value is "+name);
}

}

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);
flower.getLotus().setName("Dummy Flower");
}
}

When you run this program you can see in example output print statement "Setter Method from Lotus Called" print two time, but advice print only one times. Because Spring container does n't consider the setter value from XML file, Advice consider value only from program. Here one setter print for set value from spring.xml file and another from AopMain file.
Now If I want to use @AfterReturning advice, so that you have to change only @AfterReturning in place of @After advice.

LoggingAOP.java

package org.vtechnology.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
.....
.....
@Pointcut("within(org.vtechnology.model.Lotus)")
public void allGetter1(){
}

@AfterReturning("args(name)")
public void stringArgumentMethod(String name){
System.out.println("A Method that takes String Arguments has been
called. The value is "+name);
}

}

In this example, if any exception occur from setter method, then @AfterReturning advice will not execute because it execute only there is no exception in method. @AfterThrowing advice work if method have exception so that @AfterThrowing advice work properly. Let see that how will execute @AfterThrowing advice work

Lotus.java

package org.vtechnology.model;

public class Lotus {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
System.out.println("Setter Method from Lotus Called");
this.name = name;
throw(new RuntimeException());
}
}

context.xml

......
......

<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">
</bean>

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

LoggingAOP.java

package org.vtechnology.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
.....
.....
@Pointcut("within(org.vtechnology.model.Lotus)")
public void allGetter1(){
}

@AfterReturning("args(name)")
public void stringArgumentMethod(String name){
System.out.println("A Method that takes String Arguments has been
called. The value is "+name);
}

@AfterThrowing("args(name)")
public void exceptionAdvice(String name){
System.out.println("After Throwing Advice called. The value is "+name);
}

}

In the above example, I have seen how use @AfterReturning and @AfterThrowing advice. Now we will see that how does get the value from pointcut in runtime. So I have changed three class Lotus, LoggingAop and AopMain

Lotus.java

package org.vtechnology.model;

public class Lotus {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
System.out.println("Setter Method from Lotus Called");
this.name = name;
throw(new RuntimeException());
}

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

LoggingAOP.java

package org.vtechnology.aop;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
.....
.....
@Pointcut("within(org.vtechnology.model.Lotus)")
public void allGetter1(){
}

@AfterReturning(pointcut="args(name)", returning="returnString")
public void stringArgumentMethod(String name, String returnString){
System.out.println("A Method that takes String Arguments has been
called. The value is "+name);
System.out.println("The output value is "+returnString);
}

@AfterThrowing(pointcut="args(name)", throwing="ex")
public void exceptionAdvice(String name, RuntimeException ex){
System.out.println("An exception has been thrown "+ex);
}

}

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);
flower.getLotus().setName("Dummy Flower");
flower.getLotus().setArgumentName("Dummy Flower for Argument Name method");
}
}

In LoggingAOP, I have used returning="returnString" line with pointcut in stringArgumentMethod. returnString parameter name is same as method parameter name for using the value in methods. returning argument takes value from pointcut and assign value same method parameter name. and I have applied same process in exceptionAdvice method for throwing parameter with pointcut and assign value ex to RuntimeException class parameter.


     << Previous
Next >>