1. 程式人生 > >第七講:7.1 spring AOP介紹-前置切入

第七講:7.1 spring AOP介紹-前置切入

AOP

(面向切面程式設計)

在軟體業,AOP為Aspect Oriented Programming的縮寫,意為:面向切面程式設計,通過預編譯方式和執行期動態代理實現程式功能的統一維護的一種技術。AOP是OOP的延續,是軟體開發中的一個熱點,也是Spring框架中的一個重要內容,是函數語言程式設計的一種衍生範型。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程式的可重用性,同時提高了開發的效率。包結構如下:1,StudentService介面,package com.cruise.service;public interface StudentService {     public

 void addStudent(String name); }2,studentServiceImpl實現類,package com.cruise.service.impl;import com.cruise.service.StudentService;public class StudentServiceImpl implements StudentService{     @Override     public void addStudent(String name) {        System.out.println("新增學生:"+name);     } }3,寫beans.xml,定義studentService的bean,
xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"     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"> <bean id="studentService"
 class="com.cruise.service.impl.StudentServiceImpl">bean> beans>4,寫測試程式碼,執行測試package com.cruise.test;import org.junit.Before;import org.junit.Test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.cruise.service.StudentService;public class T {     private ClassPathXmlApplicationContext CPXAC=null;     @Before     public void setUp() throws Exception {        CPXAC= new ClassPathXmlApplicationContext("beans.xml");     }     @Test     public void test1() {        StudentService  studentService = (StudentService) CPXAC.getBean("studentService");        studentService.addStudent("張三");     } }對以上程式新增日誌 1.新建com.cruise.advice包,新建一個類StudentServiceAspect類,匯入三個切面的包(見附件),build path , doBefore()方法,獲取類名,方法名,引數列表  表結構如下:StudentServiceAspect 類如下package com.cruise.advice;import org.aspectj.lang.JoinPoint;public class StudentServiceAspect {     public void doBefor(JoinPoint jp){    System.out.println("類名:"+jp.getTarget().getClass().getName());    System.out.println("方法名:"+jp.getSignature().getName());    System.out.println("引數:"+jp.getArgs()[0]);        System.out.println("開始新增前置通知:……");     } }2,在beans.xml中,定義StudentServiceAspect類,程式碼分析: xmlns:aop="http://www.springframework.org/schema/aop" 和http://www.springframework.org/schema/aop,http://www.springframework.org/schema/aop/spring-aop.xsd">這三個需要從網上貼上,本版本是spring4,* com.cruise.service.*.*(..)表示切入的位置,第一個 * 表示任意返回值;第二個 * 表示com.cruise.service包下的任意類;第三個 * 表示任意方法;(..)表示任意引數;method="doBefor" 表示切入的具體方法,該方法一定是com.cruise.advice.StudentServiceAspect類中的一種。  xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:aop="http://www.springframework.org/schema/aop"     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"> <bean id="studentServiceAspect" class="com.cruise.advice.StudentServiceAspect">bean> <bean id="studentService" class="com.cruise.service.impl.StudentServiceImpl">bean> <aop:config>     <aop:aspect id="suibiandingyiaop" ref="studentServiceAspect">        <aop:pointcut expression="execution(* com.cruise.service.*.*(..))" id="bussnessService"/>        <aop:before method="doBefor" pointcut-ref="bussnessService"/>     aop:aspect> aop:config> beans>3.寫測試程式碼package com.cruise.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.cruise.service.StudentService;public class T {          public static void main(String[] args) {        ClassPathXmlApplicationContext CPXAC=new ClassPathXmlApplicationContext("beans.xml");        StudentService  studentService = (StudentService) CPXAC.getBean("studentService");        studentService.addStudent("張三");     } }