Spring AOP Advice - Pointcuts – Regular expression example
Last few pages talked about spring advices (before advice, after return advice and around advice). The disadvantages of
these are, these advices will intercept all available methods. What if we want to intercept only one method or two specific methods and
we dont want to intercept rest all methods?
Spring comes with a concept called Pointcuts, which allows you to intercept advices based on either method name or regular
expression.
This page gives an example for spring aop - pointcuts with regular expression match.
pom.xml file gives all required dependencies:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringJavaBasedConfig</groupId>
<artifactId>SpringJavaBasedConfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<spring.version>3.2.0.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.1</version>
</dependency>
</dependencies>
</project>
|
My business logic service class:
package com.java2novice.bean;
public class MyBusinessService {
public void runMyBusinessLogic(){
System.out.println("************************************");
System.out.println("Running business logic...");
System.out.println("************************************");
}
public void testThrowException() {
throw new NullPointerException();
}
}
|
Now create "Around Advice". Create a class which implements MethodInterceptor interface. You must call Object result = metInvocation.proceed()
method to proceed on the original method execution, else the original method will not execute.
package com.java2novice.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class ExecuteAroundMethod implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation metInvocation) throws Throwable {
System.out.println("Inside RunBeforeExecution.before() method...");
System.out.println("Running before advice...");
try{
Object result = metInvocation.proceed();
System.out.println("Inside RunAfterExecution.afterReturning() method...");
System.out.println("Running after advice...");
return result;
} catch(NullPointerException ne){
//this is for ThrowsAdvice
throw ne;
}
}
}
|
Here is the xml based configuration file. Create a bean of RegexpMethodPointcutAdvisor, and pass method name and
advisor details. In the below xml file as per the pattern, it only intercepts the method which has "Business" in it.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<bean id="busService" class="com.java2novice.bean.MyBusinessService" />
<bean id="aroundAdvice" class="com.java2novice.aop.ExecuteAroundMethod" />
<bean id="busServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean" >
<property name="target" ref="busService" />
<property name="interceptorNames">
<list>
<value>buServAdvisor</value>
</list>
</property>
</bean>
<bean id="buServAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="patterns">
<list>
<value>.*Business.*</value>
</list>
</property>
<property name="advice" ref="aroundAdvice" />
</bean>
</beans>
|
Here is the final demo class: Note that we are calling proxy bean object, not the business service bean directly.
package com.java2novice.test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java2novice.bean.MyBusinessService;
public class SpringDemo {
public static void main(String a[]){
String confFile = "applicationContext.xml";
ConfigurableApplicationContext context
= new ClassPathXmlApplicationContext(confFile);
MyBusinessService busServ = (MyBusinessService) context.getBean("busServiceProxy");
busServ.runMyBusinessLogic();
}
}
|
|