Spring Framework

Spring Projects

Spring Project 1

adplus-dvertising
Example of NameMatchMethodPointcutAdvice
Previous Home Next

Introduction: One of the disadvantage of using pointcuts is entry of configuration information solution of this problem is provided by PointcutAdvisor class.

A PointcutAdvisor class combined the functionality of a pointcut and advisor in to a single unit.

The NameMatchMethodPointcutAdvisor class combined the functionality of NameMatchMethodPointcut and DefaultPointcutAdvisor using this class the configuration information is reduced as follows.

Technology use to run this source code

  1. Spring 2.5 jar files
  2. Eclipse IDE
  3. Tomcat Server

Source Code:

applicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="t" class="org.r4r.One"/>
<bean id="a1" class="org.r4r.BeforeAdvisor"/>
<bean id="a2" class="org.r4r.AfterAdvisor"/>
<bean id="pca1" 
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="a1"/>
<property name="mappedName" value="a"/>
</bean>
<bean id="pca2" 
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="a2"/>
<property name="mappedName" value="b"/>
</bean>
<bean id="proxy" 
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="t"/>
<property name="interceptorNames">
<list>
<value>pca1</value>
<value>pca2</value></list>
</property>
</bean>
</beans>

AB.java

package org.r4r;
public interface AB {
public void a();
public void b();
}

One.java

package org.r4r;
public class One implements AB {
@Override
public void a() {
System.out.println("a() of one invoked");
}
@Override
public void b() {
System.out.println("b() of one invoked");
}
}

BeforeAdvisor.java

package org.r4r;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvisor implements MethodBeforeAdvice {
@Override
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("Before advice is applied..");
}
}

AfterAdvisor.java

package org.r4r;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterAdvisor implements AfterReturningAdvice {
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("After advisor is provide service..");
}
}

ProxyTest.java

package org.r4r;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class ProxyTest {
public static void main(String[] args) {
Resource r=new ClassPathResource("applicationContext.xml");
BeanFactory f=new XmlBeanFactory(r);
AB proxy=(AB)f.getBean("proxy");
System.out.println("Proxy obtained invoking a().");
proxy.a();
System.out.println("Proxy obtained invoking b()");
proxy.b();
}NameMatchMethodPointcut.shtml
}

output:

Proxy obtained invoking a().
Before advice is applied..
a() of one invoked
Proxy obtained invoking b()
b() of one invoked
After advisor is provide service..
Previous Home Next