Spring 入門 AOP

經過一個小例子演視怎麼使用 Spring 現實面向切面編程。java

導入 Spring 所須要的包

spring-framework-2.5.6 版須要導入如下包:
1.----- spring.jar
2.----- commons-logging.jar
3.----- aspectjrt.jar
4.----- aspectjweaver.jar
5.----- cglib-nodep-2.1_3.jar
node

spring-framework-3.2.4 版須要導入如下包:
1.----- spring-core-3.2.4.RELEASE.jar
2.----- spring-beans-3.2.4.RELEASE.jar
3.----- spring-context-3.2.4.RELEASE.jar
4.----- spring-expression-3.2.4.RELEASE.jar
5.----- commons-logging.jar
6.----- aspectjweaver.jar
7.----- aspectjrt.jar
8.----- aopalliance.jar(spring項目裏不提供,要到網上下)
spring

代碼:express

Book.java:編程

public class Book {
    private int id;
    private String name;
    //省略get set方法....    
}

 

BookService.java:app

public class BookService {
    public void save(Book book){
        System.out.println("save:"+book.getName());
    }
    
}

main方法:spa

public static void main(String[] args) {
    ApplicationContext appctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    Book b = (Book)appctx.getBean("book");
    b.setName("j2ee");
    BookService bs = (BookService)appctx.getBean("bookService");
    //調用BookService的save方法。
    bs.save(b);
    ((ClassPathXmlApplicationContext)appctx).close();
}

經過使用Spring AOP 在調用BookService的save方法先後各加上一些業務(如記錄日誌,時間等等)方便起見,這裏簡單輸出兩句話。代理

 

使用配置文件配置AOP(Schema)

添加切面類 Log.java :日誌

public class Log {
    //在目標方法執行前調用此方法,JoinPoint:封裝了目標方法的一些信息(如類名,參數等等)
    public void before(JoinPoint jp){
        
        System.out.println(jp.getSignature().getName()+":開始執行----------");
        // 獲取被代理對象
        System.out.println("被代理對象:"+jp.getTarget().getClass());
        // 獲取被代理方法
        System.out.println("被代理方法:"+jp.getSignature());
        // 獲取方法參數
        System.out.println("方法參數:"+jp.getArgs());
    }
    //在目標方法執行完後調用此方法,Object rn:目標方法的返回值(void爲null)
    public void afterreturning(JoinPoint jp,Object rn){
        System.out.println(jp.getSignature().getName()+":執行完畢----------");
        
        System.out.println(rn);
    }
}

Spring配置文件:code

<?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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <bean id="book" class="com.startspring.Book" />
    <bean id="bookService" class="com.startspring.BookService" />
    <bean id="log" class="com.startspring.aop.Log" />
    
    <aop:config>
        <!-- 指定切入點 -->
        <aop:pointcut id="bookSave" expression="execution(public void com.startspring.BookService.save(com.startspring.Book))" />
        <!-- 指定切面 -->
        <aop:aspect id="aspect" ref="log">
            <!-- 前置通知(加強):指明切入點方法執行前執行 「before」方法-->
            <aop:before method="before" pointcut-ref="bookSave"/>
            <!-- 後置通知(加強):指明切入點方法完成後執行 「afterreturning」方法,參數rn是目標方法的返回值-->
            <aop:after-returning method="afterreturning" pointcut-ref="bookSave" returning="rn"/>
        </aop:aspect>
    </aop:config>
</beans>

注:要使用aop:標籤要先引入aop命名空間(第四、七、8行)

 

使註解配置AOP(Annotation)

Spring配置文件:

<?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: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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <context:component-scan base-package="com.startspring" />
    <!-- 要使用aspect的註解,先要在配置文件中添加以下代碼:啓動自動匹配註解類-->
    <aop:aspectj-autoproxy />
    
</beans>

添加切面類 Log.java :

@Component
@Aspect  //聲明該類爲切面類
public class Log {
    //前置通知(加強):指明切入點方法執行前執行該方法
    //後面參數指定了插入點
    @Before("execution(public void com.startspring.BookService.save(com.startspring.Book))")
    public void before(JoinPoint jp){
        System.out.println(jp.getSignature().getName()+":開始執行----------");
        // 獲取被代理對象
        System.out.println("被代理對象:"+jp.getTarget().getClass());
        // 獲取被代理方法
        System.out.println("被代理方法:"+jp.getSignature());
        // 獲取方法參數
        System.out.println("方法參數:"+jp.getArgs());
    }
    
    //後置通知(加強):指明切入點方法執行完畢執行該方法
    //returning="rn" :聲明參數rn是目標方法的返回值(void爲null)
    @AfterReturning(returning="rn",value="execution(public void com.startspring.BookService.save(com.startspring.Book))")
    public void afterreturning(JoinPoint jp,Object rn){
        System.out.println(jp.getSignature().getName()+":開始完畢----------");
        System.out.println(rn);
        
    }
}

這裏Bean的聲明也採用了註解,要在Book.java和BookService.java裏添加相應的註解。或在配置文件中寫<bean>也是能夠的。

 

經過現實接口配置AOP

添加切面類 Log.java :

/*
 * 經過實現 MethodBeforeAdvice 實現前置處理。AfterReturningAdvice:後置處理。
 * 分別重寫方法:before,afterReturning
 */
public class Log implements MethodBeforeAdvice,AfterReturningAdvice{    
    
    //method:表示切入點方法.args:切入點方法的參數.target:目標對象
    public void before(Method method, Object[] args, Object target)throws Throwable {
        System.out.println(method.getName()+":開始執行----------");
        // 獲取被代理對象
        System.out.println("被代理對象:"+target);
        // 獲取被代理方法
        System.out.println("被代理方法:"+method);
        // 獲取方法參數
        System.out.println("方法參數:"+args);
        
    }
    
    //result表示目標方法的返回值
    public void afterReturning(Object result, Method method, Object[] args,Object target) throws Throwable {
        System.out.println(method.getName()+":執行完畢----------");
        
        System.out.println(result);
        
    }
}

Spring配置文件:

<?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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <bean id="book" class="com.startspring.Book" />
    <bean id="bookService" class="com.startspring.BookService" />
    <bean id="log" class="com.startspring.aop.Log" />
    
    <aop:config>
        <!-- 指定切入點 -->
        <aop:pointcut id="bookSave" expression="execution(public void com.startspring.BookService.save(com.startspring.Book))" />
        <!-- advice-ref:指定切面   pointcut-ref:指定切入點-->
        <aop:advisor advice-ref="log" pointcut-ref="bookSave"/>
    </aop:config>
</beans>
相關文章
相關標籤/搜索