The following example illustrates NameMatchMethodPointcut which is filters methods (to which the advice should be applied to) by name.
If you aren't clear about what is a pointcut, I recommend strongly to read understanding pointcut and advisor and come back here.
Theme of the application
The theme of the application is to illustrate NameMatchMethodPointcut. This application contains a SimpleBean which contains four methods.
- ding()
- ding(int)
- dong()
- dong(int)
Create a project in eclipse
- File -> New -> Project -> Java Project
- Give the project name spring42 and click Finish
- Now, the project is created.
- Under the Project Explorer (in the sidebar) you will see spring42. If you aren't able to see Project Explorer, go to Window menu -> Show view -> Project Explorer.
- Right click on the spring42 and click Properties
- On the left side, click on Java Build Path.
- Select the Libraries tab and then click on Add External Jars
- Now, add these jar files starting with: spring-core, aop-alliance, spring-aop, commons-logging
- Click on OK and then you are read
To create a class
In the Project Explorer, navigate to spring42 and right click on spring42. Then go to New -> Class and give the name of the class as defined below.
Project structure
SimpleBean.java
package spring42;
public class SimpleBean {
public void ding()
{
System.out.println("Just ding()");
}
public void ding(int x)
{
System.out.println("ding("+x+")");
}
public void dong()
{
System.out.println("Just dong()");
}
public void dong(int x)
{
System.out.println("dong("+x+")");
}
}
LoggingAdvice.java
This is an around advice which will log a message before and after the method invocation.
package spring42;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class LoggingAdvice implements MethodInterceptor {
private static Log log;
static
{
log=LogFactory.getLog(LoggingAdvice.class);
}
public Object invoke(MethodInvocation mi) throws Throwable
{
Object val=null;
// Get argument values
Object[] args=mi.getArguments();
String name=mi.getMethod().getName()+(args.length!=0?"("+args[0]+")":"()");
if(log.isInfoEnabled())
{
log.info("Invoking method "+name);
// Invoke the method now
val=mi.proceed();
log.info("Method execution completed");
}
return val;
}
}
SpringPrg.java - Main class
This is the main class. In this class, we will create a NameMatchMethodPointcut object and add the method names to which the advice should be applied.
package spring42;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
public class SpringPrg {
public static void main(String args[])
{
// Create target bean object
SimpleBean sb=new SimpleBean();
// Create a pointcut that filters methods by name
NameMatchMethodPointcut pointcut=new NameMatchMethodPointcut();
// The method to which the advice must be applied to
// must be named ding
pointcut.addMethodName("ding");
// Create an advice
Advice advice=new LoggingAdvice();
// Create an advisor which is a combination
// of pointcut and advice
Advisor advisor=new DefaultPointcutAdvisor(pointcut,advice);
// Create ProxyFactory
ProxyFactory pf=new ProxyFactory();
// Set the target bean object
pf.setTarget(sb);
// Add the advisor
pf.addAdvisor(advisor);
SimpleBean bean=(SimpleBean)pf.getProxy();
// Advice will be applied to ding()
bean.ding();
bean.ding(10);
// Advice will not be applied to dong()
bean.dong();
bean.dong(20);
}
}
Output
Apr 23, 2014 9:03:15 AM spring42.LoggingAdvice invoke
INFO: Invoking method ding()
Just ding()
Apr 23, 2014 9:03:15 AM spring42.LoggingAdvice invoke
INFO: Method execution completed
Apr 23, 2014 9:03:15 AM spring42.LoggingAdvice invoke
INFO: Invoking method ding(10)
ding(10)
Apr 23, 2014 9:03:15 AM spring42.LoggingAdvice invoke
INFO: Method execution completed
Just dong()
dong(20)
As you can see from the above output, there is no information logged before
Also see dynamic pointcut and static pointcut
dong()
and dong(20)
Also see dynamic pointcut and static pointcut
No comments:
Post a Comment