spring中的aop注解(整合junit測試)


使用spring中的aop前先來了解一下spring中aop中的一些名詞

Joimpoint(連接點):目標對象中,所有可能增強的方法

PointCut(切入點):目標對象,已經增強的方法

Advice(通知/增強):增強的代碼

Target(目標對象):被代理對象

Weaving(織入):將通知應用到切入點的過程

Proxy(代理):將通知織入到目標對象之后,形成代理對象

aspect(切面):切入點+通知

一:不使用spring的aop注解

以javaEE中的service層為例

UserService.java:被代理對象

public interface UserService {
    void save();
    void delete();
    void update();
    void find();
}

UserServiceImpl.java:被代理對象的實現類

public class UserServiceImpl implements UserService {
    @Override
    public void save() {
        System.out.println("保存用戶!");
        //int i = 1/0;
    }
    @Override
    public void delete() {
        System.out.println("刪除用戶!");
    }
    @Override
    public void update() {
        System.out.println("更新用戶!");
    }
    @Override
    public void find() {
        System.out.println("查找用戶!");
    }
}

MyAdvice.java:通知,增強的代碼

//通知類
public class MyAdvice {
    
    //前置通知    
//        |-目標方法運行之前調用
    //后置通知(如果出現異常不會調用)
//        |-在目標方法運行之后調用
    //環繞通知
//        |-在目標方法之前和之后都調用
    //異常攔截通知
//        |-如果出現異常,就會調用
    //后置通知(無論是否出現 異常都會調用)
//        |-在目標方法運行之后調用
//----------------------------------------------------------------
    //前置通知
    public void before(){
        System.out.println("這是前置通知!!");
    }
    //后置通知
    public void afterReturning(){
        System.out.println("這是后置通知(如果出現異常不會調用)!!");
    }
    //環繞通知
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("這是環繞通知之前的部分!!");
        Object proceed = pjp.proceed();//調用目標方法
        System.out.println("這是環繞通知之后的部分!!");
        return proceed;
    }
    //異常通知
    public void afterException(){
        System.out.println("出事啦!出現異常了!!");
    }
    //后置通知
    public void after(){
        System.out.println("這是后置通知(出現異常也會調用)!!");
    }
}

applicationContext.xml

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

<!-- 准備工作: 導入aop(約束)命名空間 -->
<!-- 1.配置目標對象 -->
    <bean name="userService" class="cn.itcast.service.UserServiceImpl" ></bean>
<!-- 2.配置通知對象 -->
    <bean name="myAdvice" class="cn.itcast.d_springaop.MyAdvice" ></bean>
<!-- 3.配置將通知織入目標對象 -->
    <aop:config>
        <!-- 配置切入點 
            public void cn.itcast.service.UserServiceImpl.save() 
            void cn.itcast.service.UserServiceImpl.save()
            * cn.itcast.service.UserServiceImpl.save()
            * cn.itcast.service.UserServiceImpl.*()
            
            * cn.itcast.service.*ServiceImpl.*(..)
            * cn.itcast.service..*ServiceImpl.*(..)
        -->
        <aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="pc"/>
        <aop:aspect ref="myAdvice" >
            <!-- 指定名為before方法作為前置通知 -->
            <aop:before method="before" pointcut-ref="pc" />
            <!-- 后置 -->
            <aop:after-returning method="afterReturning" pointcut-ref="pc" />
            <!-- 環繞通知 -->
            <aop:around method="around" pointcut-ref="pc" />
            <!-- 異常攔截通知 -->
            <aop:after-throwing method="afterException" pointcut-ref="pc"/>
            <!-- 后置 -->
            <aop:after method="after" pointcut-ref="pc"/>
        </aop:aspect>
    </aop:config>
</beans>

Demo.java:整個junit的aop測試類

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:cn/itcast/d_springaop/applicationContext.xml")
public class Demo {
    @Resource(name="userService")
    private UserService us;
    
    @Test
    public void fun1(){
        us.save();
    }  
}

二:使用spring的aop注解

以javaEE中的service層為例

UserService.java:被代理對象

public interface UserService {
    void save(); void delete(); void update(); void find(); }

UserServiceImpl.java:被代理對象的實現類

public class UserServiceImpl implements UserService {
    @Override
    public void save() {
        System.out.println("保存用戶!");
        //int i = 1/0;
    }
    @Override
    public void delete() {
        System.out.println("刪除用戶!");
    }
    @Override
    public void update() {
        System.out.println("更新用戶!");
    }
    @Override
    public void find() {
        System.out.println("查找用戶!");
    }
}

MyAdvice.java:通知類

//通知類
@Aspect
//表示該類是一個通知類
public class MyAdvice {
    @Pointcut("execution(* cn.itcast.service.*ServiceImpl.*(..))")
    public void pc(){}
    //前置通知
    //指定該方法是前置通知,並制定切入點
    @Before("MyAdvice.pc()")
    public void before(){
        System.out.println("這是前置通知!!");
    }
    //后置通知
    @AfterReturning("execution(* cn.itcast.service.*ServiceImpl.*(..))")
    public void afterReturning(){
        System.out.println("這是后置通知(如果出現異常不會調用)!!");
    }
    //環繞通知
    @Around("execution(* cn.itcast.service.*ServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("這是環繞通知之前的部分!!");
        Object proceed = pjp.proceed();//調用目標方法
        System.out.println("這是環繞通知之后的部分!!");
        return proceed;
    }
    //異常通知
    @AfterThrowing("execution(* cn.itcast.service.*ServiceImpl.*(..))")
    public void afterException(){
        System.out.println("出事啦!出現異常了!!");
    }
    //后置通知
    @After("execution(* cn.itcast.service.*ServiceImpl.*(..))")
    public void after(){
        System.out.println("這是后置通知(出現異常也會調用)!!");
    }
}

applicationContext.xml

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

<!-- 准備工作: 導入aop(約束)命名空間 -->
<!-- 1.配置目標對象 -->
    <bean name="userService" class="cn.itcast.service.UserServiceImpl" ></bean>
<!-- 2.配置通知對象 -->
    <bean name="myAdvice" class="cn.itcast.e_annotationaop.MyAdvice" ></bean>
<!-- 3.開啟使用注解完成織入 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

Demo.java:測試類

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:cn/itcast/e_annotationaop/applicationContext.xml")
public class Demo {
    @Resource(name="userService")
    private UserService us;
    
    @Test
    public void fun1(){
        us.save();
    }
    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM