dream_sky




  种一棵树最好的时间是  十年前。      其次是,     现在!

Spring-AOP解析

策略模式:选择动态代理还是CGLIB方式:

1、这种在运行时,动态地将代码切入到类的指定方法、指定位置上的编程思想就是面向切面的编程。

2、AOP基本上是通过代理机制实现的

3、写好验证用户的代码,然后告诉Spring你要把这段代码加到哪几个地方(execution处),Spring就会帮你加过去,而不要你自己Copy过去

4、在与mybatis的结合中,可以通过配置AOP生成事务代理,<tx:advice>配置AOP中的通知。

<aop:advisor advice-ref="txAdvice"  

            pointcut-ref="interceptorPointCuts" /> 

4、注解的方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Aspect
public class Log {
    @Before("execution(* cn.sxt.service.impl.*.*(..))")
    public void before(){
        System.out.println("-----方法执行前-----");
    }
    @After("execution(* cn.sxt.service.impl.*.*(..))")
    public void after(){
        System.out.println("-----方法执行后-----");
    }
    @Around("execution(* cn.sxt.service.impl.*.*(..))")
    public Object aroud(ProceedingJoinPoint jp) throws Throwable{
        System.out.println("环绕前");
        System.out.println("签名:"+jp.getSignature());
        //执行目标方法
         Object result = jp.proceed();
        System.out.println("环绕后");
        return result;
    }
}
<wiz_tmp_tag class="wiz-block-scroll">
 

 

1
2
3
4
5
6
7
8
9
<bean id="log" class="cn.sxt.log.Log"/>
<bean id="afterLog" class="cn.sxt.log.AfterLog"/>
 <!--<bean id="exceptionlog" class="cn".sxt.log.ExceptionLog></bean> -->
<aop:config>
    <aop:pointcut expression="execution(* cn.sxt.service.impl.*.*(..))" id="pointcut"/>
     <!--<aop:advisor advice-ref="exceptionlog" pointcut-ref="pointcut"/> -->
    <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
    <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>

 

 

1
2
3
4
5
6
7
8
9
10
<bean id="userService" class="cn.sxt.service.impl.UserServiceImpl"/>
<bean id="log" class="cn.sxt.log.Log"/>
<aop:aspectj-autoproxy/>
<!--<aop:config>-->
    <!--<aop:aspect ref="log">-->
        <!--<aop:pointcut expression="execution(* cn.sxt.service.impl.*.*(..))" id="pointcut"/>-->
        <!--<aop:before method="before" pointcut-ref="pointcut"/>-->
        <!--<aop:after method="after" pointcut-ref="pointcut"/>-->
    <!--</aop:aspect>-->
<!--</aop:config>-->

 

posted @ 2018-06-13 15:50  书晨007  阅读(181)  评论(0编辑  收藏  举报
将时间精确到分,将获得较高的效率!