學習Spring AOP

AOP(Aspect Oriented Program)面向切面編程;

1、理解Spring AOP 的基本術語;
切面(Aspect):切面是對象操作過程中的截面,通俗的說,事務、日誌、安全性的框架、權限等就是切面。

通知(Advice):通知是某個切入點被橫切後,所採取的處理邏輯,一般來說,切面中的方法就是通知。

切入點(Pointcut):切面注入到程序中的位置,只有符合切入點的條件,才能讓通知和目標方法結合在一起。(自己理解爲目標類中的所有方法的位置(前、後、環繞)都可以成爲切入點)

連接點(Join Point):對象操作過程中的某個階段點,它實際上是對象的一個操作。
2、AOP的意義
a、在開發過程中,日誌、權限、安全性框架、目標方法完全是松耦合的;
b、在形成代理對象的方法的過程中就把這幾個結合在一起了。
配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop"
       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-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id='persondao' class='com.xkang.dao.impl.PersonDao'></bean>
<bean id='personService' class='com.xkang.seevice.impl.Personservice'>
    <property name='personDao' ref="persondao">

    </property>

</bean>
<bean id='logger' class='com.xkang.aspect.Logger'></bean>
<!--切面配置-->  
<aop:config>
<!-- 切入點配置  expression 切入點表達式  指明com.xkang.seevice.impl.Personservice類下的所有方法-->
    <aop:pointcut expression="execution(* com.xkang.seevice.impl.Personservice.*(..))" id="perform"/>
    <aop:aspect ref="logger"><!-- ref 指定logger爲切面 -->
        <!-- 環繞通知    method 切面中的方法  pointcut-ref指向切面 -->
        <aop:around method="checkOut" pointcut-ref="perform"/>
    </aop:aspect>
    <aop:aspect ref="logger">

        <aop:after-throwing method="logging" pointcut-ref="perform" throwing="ex"/>
    </aop:aspect>
</aop:config>
 </beans>   

目標類:

package com.xkang.seevice.impl;
import com.xkang.dao.impl.PersonDao;
public class Personservice {
    private PersonDao personDao;
    public PersonDao getPersonDao() {
        return personDao;
    }
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
    public String  savePerson(){

        personDao.savePerson();
        return "aaa";
    }
}

切面類:

package com.xkang.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

public class Logger {
    public void logging(Throwable ex){

        System.out.println(ex.getMessage());

    }
    public void checkOut(ProceedingJoinPoint joinpoint) throws Throwable{
        //Object obj = null;
        System.out.println("環繞通知開始了");
        joinpoint.proceed();
        System.out.println("環繞通知結束了");

    }
}

測試方法:

@Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Personservice wife=(Personservice)context.getBean("personService");
        String name = wife.savePerson();
        System.out.println(name);   
    }

程序運行結果:

環繞通知開始了
save Person
環繞通知結束了
null

將切面類改爲:

package com.xkang.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

public class Logger {
    public void logging(Throwable ex){

        System.out.println(ex.getMessage());    
    }
    public Object checkOut(ProceedingJoinPoint joinpoint) throws Throwable{
        Object obj = null;
        System.out.println("環繞通知開始了");
        obj = joinpoint.proceed();
        System.out.println("環繞通知結束了");
        //System.out.println(obj);
        return obj;
    }
}

程序運行結果:

環繞通知開始了
save Person
環繞通知結束了
aaa

說明:
AOP中,context.getBean()方法得到的是一個代理對象,並沒有得到一個真正對象。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章