A composable pointcuts can be composed using the static methods in the org.springframework.aop.support.Pointcuts class or using composable pointcut class in the same package.
MainClaz.java
package roseindia.net.union;
import java.lang.reflect.Method;
import org.aopalliance.aop.Advice;
import org.springframework.aop.Advisor;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcher;
public class MainClaz {
ComposablePointcut composablePointcut = new ComposablePointcut(
ClassFilter.TRUE);
public static void test(SimpleBean proxy) {
proxy.getAge();
proxy.setName("Vinay");
proxy.getName();
}
public static SimpleBean getProxy(ComposablePointcut composablePointcut,
SimpleBean simpleBean) {
Advice advice = new TestAdvice();
Advisor advisor = new DefaultPointcutAdvisor(composablePointcut, advice);
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.addAdvice(advice);
proxyFactory.setTarget(simpleBean);
SimpleBean proxy = (SimpleBean) proxyFactory.getProxy();
return proxy;
}
public static void main(String[] args) {
SimpleBean simpleBean = new SimpleBean();
ComposablePointcut composablePointcut = new ComposablePointcut(
ClassFilter.TRUE, new GetterMethodMatcher());
composablePointcut.union(new SetterMethodMatcher());
SimpleBean proxy = getProxy(composablePointcut, simpleBean);
test(proxy);
}
}
TestAdvice.java
class TestAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object object)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("Before " + method);
}
}
GetterMethodMatcher .java
class GetterMethodMatcher extends StaticMethodMatcher {
@Override
public boolean matches(Method method, Class claz) {
// TODO Auto-generated method stub
boolean result = method.getName().startsWith("get");
return false;
}
}
SetterMethodMatcher .java
class SetterMethodMatcher extends StaticMethodMatcher {
@Override
public boolean matches(Method method, Class claz) {
// TODO Auto-generated method stub
boolean result = method.getName().startsWith("get");
return result;
}
}
SimpleBean .java
class SimpleBean {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
|
Before public int roseindia.net.union.SimpleBean.getAge() Before public void roseindia.net.union.SimpleBean.setName(java.lang.String) Before public java.lang.String roseindia.net.union.SimpleBean.getName() |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.