1. 程式人生 > >AspectJ註解版和XML版

AspectJ註解版和XML版

post 規範 {} spring imp sele aik update sel

什麽是AspectJ?

AspectJ是一個面向切面的框架,它擴展了Java語言。AspectJ定義了AOP語法,所以它有一個專門的編譯器用來生成遵守Java字節編碼規範的Class文件。

Aspect註解版

AspectJ自動代理

1.在xml文件中配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--目標類型-->
<bean id="service" class="cn.happy.Aspect01.UserServiceImpl"></bean>

    <!--增強-->
    <bean id="MyAspect" class="cn.happy.Aspect01.UserTest"></bean>

    <!--Aspectj自動代理-->
    <aop:aspectj-autoproxy/>


</beans>

  

2.創建接口、類

package cn.happy.Aspect01;

/**
 * Created by Administrator on 2018/3/10.
 */
public interface IUserService {
    void select();
    void update();
}

  

package cn.happy.Aspect01;

/**
 * Created by Administrator on 2018/3/10.
 */
public class UserServiceImpl implements IUserService {
    public void select() {
        System.out.println("select OK!");
    }

    public void update() {
        int i=5/0;
        System.out.println("update OK!");
    }
}

  

UserTest類

package cn.happy.Aspect01;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * Created by Administrator on 2018/3/10.
 */
@Aspect
public class UserTest {
    //前置增強
    /*@Before("execution(* *..Aspect01.*.select(..))")
    public void MyAspectBefore(){
        System.out.println("Before===");
    }*/


    //後置增強
    /*@AfterReturning("execution(* *..Aspect01.*.select(..))")
    public void MyAspectAfter(){
        System.out.println("After===");
    }*/

    /*//環繞增強
    @Around("execution(* *..Aspect01.*.select(..))")
    public void MyAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("環繞增強A");
        pjp.proceed();
        System.out.println("環繞增強B");
    }*/

    /*//異常增強
    @AfterThrowing("execution(* *..Aspect01.*.update(..))")
    public void MyAfterThrowing(){
        System.out.println("網絡異常");
    }*/
  //最終增強
    @After("execution(* *..Aspect01.*.update(..))")
    public void MyAfter(){
        System.out.println("最終增強");
    }

    //PointCut註解
    @Pointcut(value="execution(* *..Aspect01.*.select(..))")
    public void selects(){}

    @Pointcut(value="execution(* *..Aspect01.*.update(..))")
    public void update(){}

    @AfterReturning("selects()||update()")
    public void MyPointcutAfter(){
        System.out.println("After===");
    }
}

  

AspectJ XML版

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--目標類型-->
<bean id="service" class="cn.happy.Aspect02.IUserServiceImpl"></bean>

    <!--增強-->
    <bean id="MyAspect" class="cn.happy.Aspect02.UserTest"></bean>

   <aop:config>
       <!--切點-->
       <aop:pointcut id="MyPointCut" expression="execution(* *..Aspect02.*.select(..))"/>
       <!--切面Aspect-->
       <aop:aspect ref="MyAspect">
           <!--前置增強-->
           <aop:before method="select" pointcut-ref="MyPointCut"/>

            <!--後置增強-->
           <aop:after-returning method="After" pointcut-ref="MyPointCut"/>

           <!--環繞增強-->
            <aop:around method="MyAround" pointcut-ref="MyPointCut"/>

           <!--異常增強-->
           <aop:after-throwing method="MyThrowing" pointcut-ref="MyPointCut"/>

           <!--<!–最終增強–>
           <aop:after method="MyAfter" pointcut-ref="MyPointCut"/>-->

       </aop:aspect>
   </aop:config>


</beans>

  

AspectJ註解版和XML版