分别使用Xml和注解来配置Spring

JoinPoint  ->   ProceedingJoingpoint
一:使用注解配置Spring:
    1. 启用AspectJ支持
@Configuration
@EnableAspectJAutoProxy
public class AppConfig {

}

2. 声明切面:   一些横切性的关注点
@Aspect
@Component
public class NotVeryUsefulAspect {

}
3. 声明切入点表达式
@Pointcut("execution(* com.yc.*.*(..))")
private void anyOldTransfer() {}
4. 声明增强
    @Before("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")
    public void doAccessCheck() {
        // ...
    }

   注意点:  Around一定要加  ProceedingJointPoint参数
  public Object xxx(){
    xxxx
    Object returnValue=pjp.proceed();
    yyy
    return returnValue;
   }

   2。在任何增强的方法中,如果要获取联接点的信息,则方法中加入参数JoinPoint

当有多个切面时,执行的顺序:   可以通过  Ordered接口或@Order来设定,   值小的伏先级高,



应用:  当对系统进行扩展.

=========================================================================
二:使用XML配置Spring:
1. 启用AspectJ支持
<aop:aspectj-autoproxy/>
2。 声明切面:
<aop:config>
    <aop:aspect id="myAspect" ref="aBean">
        ...
    </aop:aspect>
</aop:config>

<bean id="aBean" class="...">
    ...
</bean>
3. 声明切入点表达式
 <aop:pointcut id="businessService"
        expression="execution(* com.xyz.myapp.service.*.*(..))"/>
4. 声明增强
  <aop:before
        pointcut-ref="dataAccessOperation"
        method="doAccessCheck"/>






posted @ 2017-09-17 22:16  程序猿的前进之路  阅读(304)  评论(0编辑  收藏  举报