画布 1
如何正确使用AOP
  AspectJ 风格的注解
    激活方式
      @Configuration
@EnableAspectJAutoProxy
public class AppConfig {
}
      <aop:aspectj-autoproxy/>
    使用方法
      注解方式
        package org.xyz;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class NotVeryUsefulAspect {
}
        // 直接声明切点，方法签名都是 void，这个声明要要被引用，直接用名称 anyOldTransfer - 用一个方法来设计切点变量是一种设计思想
@Pointcut("execution(* transfer(..))") // the pointcut expression
private void anyOldTransfer() {} // the pointcut signature
// 切点里加上 advice
@Around("execution(public * package..*.*(..))")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {}
  xml schema 方式 <aop:config> 元素
    使用 advisor
      <aop:config>
<!-- -->
<aop:advisor
pointcut="com.xyz.someapp.SystemArchitecture.businessService()"
advice-ref="tx-advice"/>
</aop:config>
<tx:advice id="tx-advice">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
      RegexpMethodPointcutAdvisor
      cache:advice
    使用自定义 aspect
      <aop:config proxy-target-class="true">
<aop:pointcut id="pointcut" expression="execution(* com.lcifn.spring.aop.bean.*.*(..)) and args(str,date,..)"/>
<aop:aspect ref="advice">
<aop:around method="aroundIntercept" pointcut-ref="pointcut" arg-names="str,date"/>
</aop:aspect>
</aop:config>
        aop:aspect 注解并不会自然地理解 @Aspect 相关的注解
    XXXProxyCreator bean: Target property + Interceptor property
      BeanNameAutoProxyCreator
      DefaultAdvisorAutoProxyCreator
    aop:pointcut
      JdkRegexpMethodPointcut
      ControlFlowPointcut
    <aop:before pointcut-ref="businessService" method="monitor"/>
    <aop:declare-parents
  ProxyCreatorSupport
    ProxyFactory
      ProxyFactory factory = new ProxyFactory(new SimplePojo());
factory.addInterface(Pojo.class);
factory.addAdvice(new RetryAdvice());
factory.addAdvisor(myAdvisor);
Pojo pojo = (Pojo) factory.getProxy();
// this is a method call on the proxy!
pojo.foo();
        MethodBeforeAdvice
        AfterAdvice
    AspectJProxyFactory
      // create a factory that can generate a proxy for the given target object
AspectJProxyFactory factory = new AspectJProxyFactory(targetObject);
// add an aspect, the class must be an @AspectJ aspect
// you can call this as many times as you need with different aspects
factory.addAspect(SecurityManager.class);
// you can also add existing aspect instances, the type of the object supplied must be an @AspectJ aspect
factory.addAspect(usageTracker);
// now get the proxy object...
MyInterfaceType proxy = factory.getProxy();
  ProxyFactoryBean
    <bean id="loggerPerson" class="LoggerPerson"/>
    <bean id="pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="patterns">
<list>
<value>.*ay.*</value>
<value>.*ie</value>
</list>
</property>
</bean>
    <!-- DefaultPointcutAdvisor 就是典型的 advice api + pointcut api -->
<bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="pointcut" ref="pointcut"/>
<property name="advice" ref="loggerPerson"/>
</bean>
    <bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor">
</bean>
    <bean id="ProxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref bean="person"/>
</property>
<property name="interceptorNames">
<list>
<value>advisor</value>
<value>debugInterceptor</value>
</list>
</property>
</bean>
      HotSwappableTargetSource
target 可切换
    <bean id="person" class="Person"/>
  1 只有 singleton instantiation model
2 它不能表达 composite pointcut
  要编程式地使用 advisor 可以这样做，否则还是用 aspect 为妙。
ProxyFactoryBean is combining Spring AOP with Spring's IoC container. 所以应该使用 bean 声明的方式来获得代理。
  ProxyFactory is independently of Spring's IoC container. 所以可以直接写一个 main 来生成 proxy。
可能很多人注意到了，每个 Factory 对应一个 target，产出 advised，所以 ProxyFactory 的 AdvisedCreator 的子类