Java Tutorial/Spring/Pointcut

Материал из Java эксперт
Перейти к: навигация, поиск

All Kinds Of Pointcut

File: context.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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd">
    <bean id="test" class="bean.TestBean2">
        <property name="simpleBean" ref="simple"/>
    </bean>
    <bean id="simple" class="bean.SimpleBean"/>
    <aop:config>
        <aop:advisor advice-ref="tx-advice"
                     pointcut="SystemPointcuts.testBeanExecution()"/>
    </aop:config>
    <bean id="transactionManager" class="NoopTransactionManager"/>
    <tx:advice id="tx-advice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/> 
        </tx:attributes>
    </tx:advice>
</beans>





Class Filter In DynamicMethodMatcherPointcut

File: Main.java



import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.Advisor;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.DynamicMethodMatcherPointcut;
public class Main {
  public static void main(String[] args) {
    MyClass target = new MyClass();
    Advisor advisor = new DefaultPointcutAdvisor(new SimpleDynamicPointcut(), new SimpleAdvice());
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(target);
    pf.addAdvisor(advisor);
    MyClass proxy = (MyClass) pf.getProxy();
    proxy.foo(1);
    proxy.bar();
  }
}
class MyClass {
  public void foo(int x) {
    System.out.println("MyClass.foo() : " + x);
  }
  public void bar() {
    System.out.println("MyClass.bar()");
  }
}
class SimpleAdvice implements MethodInterceptor {
  public Object invoke(MethodInvocation invocation) throws Throwable {
    System.out.println("SimpleAdivce.Invoking " + invocation.getMethod().getName());
    Object retVal = invocation.proceed();
    System.out.println("SimpleAdvice.Done");
    return retVal;
  }
}
class SimpleDynamicPointcut extends DynamicMethodMatcherPointcut {
  public boolean matches(Method method, Class cls) {
    return ("foo".equals(method.getName()));
  }
  public boolean matches(Method method, Class cls, Object[] args) {
    int x = (Integer) args[0];
    return (x != 100);
  }
  public ClassFilter getClassFilter() {
    return new ClassFilter() {
      public boolean matches(Class cls) {
        return (cls == MyClass.class);
      }
    };
  }
}





ComposablePointcut Intersection

File: Main.java



import java.lang.reflect.Method;
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.ruposablePointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcher;
public class Main {
  public static void main(String[] args) {
    SampleBean target = new SampleBean();
    ComposablePointcut pc = new ComposablePointcut(ClassFilter.TRUE, new GetterMethodMatcher());

    pc.intersection(new GetAgeMethodMatcher());
    SampleBean proxy = getProxy(pc, target);
    testInvoke(proxy);    
  }
  private static SampleBean getProxy(ComposablePointcut pc, SampleBean target) {
    Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleBeforeAdvice());
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(target);
    pf.addAdvisor(advisor);
    return (SampleBean) pf.getProxy();
  }
  private static void testInvoke(SampleBean proxy) {
    proxy.getAge();
    proxy.getName();
    proxy.setName("QQQ");
  }
}
class GetterMethodMatcher extends StaticMethodMatcher {
  public boolean matches(Method method, Class cls) {
    return (method.getName().startsWith("get"));
  }
}

class GetAgeMethodMatcher extends StaticMethodMatcher {
  public boolean matches(Method method, Class cls) {
    return "getAge".equals(method.getName());
  }
}
class SampleBean {
  public String getName() {
    return "AAA";
  }
  public void setName(String name) {
  }
  public int getAge() {
    return 100;
  }
}
class SimpleBeforeAdvice implements MethodBeforeAdvice {
  public void before(Method method, Object[] args, Object target) throws Throwable {
    System.out.println("Before method " + method);
  }
}





ComposablePointcut Union

File: Main.java



import java.lang.reflect.Method;
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.ruposablePointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcher;
public class Main {
  public static void main(String[] args) {
    SampleBean target = new SampleBean();
    ComposablePointcut pc = new ComposablePointcut(ClassFilter.TRUE, new GetterMethodMatcher());

    pc.union(new SetterMethodMatcher());
    SampleBean proxy = getProxy(pc, target);
    testInvoke(proxy);
  }
  private static SampleBean getProxy(ComposablePointcut pc, SampleBean target) {
    Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleBeforeAdvice());
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(target);
    pf.addAdvisor(advisor);
    return (SampleBean) pf.getProxy();
  }
  private static void testInvoke(SampleBean proxy) {
    proxy.getAge();
    proxy.getName();
    proxy.setName("QQQ");
  }
}
class GetterMethodMatcher extends StaticMethodMatcher {
  public boolean matches(Method method, Class cls) {
    return (method.getName().startsWith("get"));
  }
}
class SetterMethodMatcher extends StaticMethodMatcher {
  public boolean matches(Method method, Class cls) {
    return (method.getName().startsWith("set"));
  }
}
class SampleBean {
  public String getName() {
    return "AAA";
  }
  public void setName(String name) {
  }
  public int getAge() {
    return 100;
  }
}
class SimpleBeforeAdvice implements MethodBeforeAdvice {
  public void before(Method method, Object[] args, Object target) throws Throwable {
    System.out.println("Before method " + method);
  }
}





Control Flow Pointcut

File: Main.java



import java.lang.reflect.Method;
import org.springframework.aop.Advisor;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.ControlFlowPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
public class Main {
  public static void main(String[] args) {
    TestBean target = new TestBean();
    Pointcut pc = new ControlFlowPointcut(Main.class, "test");
    Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleBeforeAdvice());
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(target);
    pf.addAdvisor(advisor);
    TestBean proxy = (TestBean) pf.getProxy();
    proxy.foo();
    test(proxy);
  }
  static void test(TestBean bean) {
    bean.foo();
  }
}
class TestBean {
  public void foo() {
    System.out.println("foo");
  }
}
class SimpleBeforeAdvice implements MethodBeforeAdvice {
  public void before(Method method, Object[] args, Object target) throws Throwable {
    System.out.println("Before method " + method);
  }
}





Default Pointcut Advisor

File: Main.java



import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import bean.MyClass;
import bean.SimpleAfterAdvice;
public class Main {
  public static void main(String[] args) {
    MyClass target = new MyClass();
    AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
    pc.setExpression("execution(* bean..*.get*(..))");
    Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleAfterAdvice());
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(target);
    pf.addAdvisor(advisor);
    MyClass proxy = (MyClass) pf.getProxy();
    System.out.println(proxy.getName());
    proxy.setName("New Name");
    System.out.println(proxy.getHeight());
  }
}





Jdk Regexp based Method Pointcut

File: Main.java



import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
public class Main {
  public static void main(String[] args) {
      RegexpBean target = new RegexpBean();
      JdkRegexpMethodPointcut pc = new JdkRegexpMethodPointcut();
      pc.setPattern(".*foo.*");
      Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleAdvice());
      ProxyFactory pf = new ProxyFactory();
      pf.setTarget(target);
      pf.addAdvisor(advisor);
      RegexpBean proxy = (RegexpBean)pf.getProxy();
      proxy.foo1();
      proxy.foo2();
      proxy.bar();
  }
}
class SimpleAdvice implements MethodInterceptor {
  public Object invoke(MethodInvocation invocation) throws Throwable {
      System.out.println("SimpleAdvice.Invoking:" + invocation.getMethod().getName());
      Object retVal = invocation.proceed();
      System.out.println("SimpleAdvice.Done");
      return retVal;
  }
}
class RegexpBean {
  public void foo1() {
      System.out.println("RegexpBean.foo1");
  }
  public void foo2() {
      System.out.println("RegexpBean.foo2");
  }
  public void bar() {
      System.out.println("RegexpBean.bar");
  }
}





NameMatchMethodPointcut

File: Main.java



import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
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 Main {
  public static void main(String[] args){
    MyClass target = new MyClass();
    NameMatchMethodPointcut pc = new NameMatchMethodPointcut();
    pc.addMethodName("foo");
    pc.addMethodName("bar");
    Advisor advisor = new DefaultPointcutAdvisor(pc, new SimpleAdvice());
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(target);
    pf.addAdvisor(advisor);
    MyClass proxy = (MyClass)pf.getProxy();
    proxy.foo();
    proxy.foo(1);
    proxy.bar();
    proxy.yup();
  }
}
class SimpleAdvice implements MethodInterceptor {
  public Object invoke(MethodInvocation invocation) throws Throwable {
      System.out.println("start: " + invocation.getMethod().getName());
      Object retVal = invocation.proceed();
      System.out.println("end");
      return retVal;
  }
}
class MyClass {
  public void foo() {
      System.out.println("foo");
  }
  public void foo(int x) {
      System.out.println("foo " + x);
  }
  public void bar() {
      System.out.println("bar");
  }
  public void yup() {
      System.out.println("yup");
  }
}





NameMatchMethodPointcutAdvisor

File: context.xml



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="afterBean" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target">
      <bean class="MtBean">
        <property name="firstName" value="AAA"/>
      </bean>
    </property>
    <property name="interceptorNames">
      <list>
        <idref bean="endMatchPointcutAdvisor"/>
      </list>
    </property>
    <property name="proxyTargetClass" value="true"/>
  </bean>
  <bean id="endMatchPointcutAdvisor"
        class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"
        singleton="false">
    <property name="advice" ref="concurrencyThrottleInterceptor"/>
    <property name="mappedName" value="showValues"/>
  </bean>
  <bean id="concurrencyThrottleInterceptor"
        class="org.springframework.aop.interceptor.ConcurrencyThrottleInterceptor"
        singleton="false">
    <property name="concurrencyLimit" value="5"/>
  </bean>
</beans>