In this example you will learn how use static pointcut of Spring AOP.
In this example you will learn how use static pointcut of Spring AOP.Static pointcuts are based on method and target class, they cannot take into account the methods arguments.
SimpleBean.java
package roseindia.net.pointcut; public class SimpleBean { public void greetMethod() { System.out.println("Have a Peaceful Day"); } }
SimpleAdvice.java
package roseindia.net.pointcut; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class SimpleAdvice implements MethodInterceptor { @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { // TODO Auto-generated method stub System.out.println("Calling " + methodInvocation.getMethod().getName()); Object obj = methodInvocation.proceed(); System.out.println("proceed..........."); return obj; } }
SimplePointCut.java
package roseindia.net.pointcut; import java.lang.reflect.*; import org.springframework.aop.ClassFilter; import org.springframework.aop.support.ClassFilters; import org.springframework.aop.support.StaticMethodMatcherPointcut; public class SimplePointCut extends StaticMethodMatcherPointcut { @Override public boolean matches(Method method, Class clazz) { // TODO Auto-generated method stub return ("greetMethod".equals(method.getName())); } }
TestPointCut.java
package roseindia.net.pointcut; import java.lang.reflect.Proxy; import org.aopalliance.aop.Advice; import org.springframework.aop.Advisor; import org.springframework.aop.ClassFilter; import org.springframework.aop.MethodMatcher; import org.springframework.aop.Pointcut; import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.support.DefaultPointcutAdvisor; import org.springframework.aop.support.Pointcuts; public class TestPointCut { public static void main(String[] args) { SimpleBean simpleBean = new SimpleBean(); SimpleBean beanProxy; Pointcut pointcut = new SimplePointCut(); Advice advice = new SimpleAdvice(); Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.addAdvisor(advisor); proxyFactory.setTarget(simpleBean); beanProxy = (SimpleBean) proxyFactory.getProxy(); beanProxy.greetMethod(); } }
Calling greetMethod Have a Peaceful Day proceed........... |