Spring入門Blog[七、Spring Aop的理解和簡單實現] – JAVA編程語言程序開發技術文章

1、AOP概念

所說的面向切面編程其實就是在處理一系列業務邏輯的時候這一系列動作看成一個動作集合。比如連接數據庫來說:

加載驅動—–獲取class——–獲取連接對象——-訪問數據庫——查詢———操作結果

對於上面的這一系列動作我們把其中的虛線看成是一個個的切面。然後我們在虛線的位置上加入一些邏輯。哪怕是日志,這也就成就瞭在不知不覺中將邏輯處理加入到瞭相應的位置上。而形成瞭所謂的面向切面編程!

 

下面通過@Before演示Aop織入到方法之前執行一些邏輯

[html] 
AOP的應用: 
Xml配置  這裡的xml配置引入瞭aop的xsd語法約束。另外加入的aop的自動代理標簽。請看註釋 
<?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:annotation-config/> 
    <!– 配置容器資源掃描的包 –> 
   <context:component-scan base-package="com.spring" /> 
    <!– 自動產生代理,內部實現是用AspectJ實現。可以使用aspectj的註解產生代理 –> 
    <aop:aspectj-autoproxy /> 
</beans> 

加入Jar包:


1、編寫被註入的方法。必須得是spring容器管理的對象

[java] 
import org.springframework.stereotype.Component; 
import com.spring.dao.UserDao; 
 
@Component("userService") 
public class UserServiceImpl implements UserService{ 
    private UserDao userDao; 
     
    public void setUserDao(UserDao userDao) { 
        this.userDao = userDao; 
    } 
     
    //在下面方法前面加邏輯 
    public void HelloWorld(){ 
        System.out.println("helloworld"); 
    } 

2、織入方法的配置:

[java] 
package com.spring.aop; 
 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.springframework.stereotype.Component; 
 
//聲明切面 
@Aspect 
//加入Spring管理容器 
@Component("LogInterceptor") 
public class LogInterceptor { 
    //指定織入的方法。 
    @Before("execution(public void com.spring.service.UserServiceImpl.HelloWorld())") 
    public void BeforeMethod(){ 
        System.out.println("method start!"); 
    } 

 
測試aop的織入: 
public class SpringTest { 
    @Test 
    public void test01() { 
        BeanFactory applicationContext = new ClassPathXmlApplicationContext( 
                "beans.xml"); 
        UserService user = (UserService) applicationContext.getBean("userService"); 
        user.HelloWorld(); 
    } 

 

註意:

在編寫過程中的織入點語法上指定before織入哪個方法的前面。而括號中的這個被指定的織入點最好是實現一個接口。如果不實現接口的話就會報異常為:
Cannot proxy target class because CGLIB2 is not available.Add CGLIB to the class path or specify proxy interfaces
這個異常的解決辦法為:

加入cglib.jar。因為被織入對象的方法單位如果沒有實現接口的話它就需要cglib.jar的支持。

 

AOP基本概念:

JoinPoint:切入點、可以理解為上面案例的HelloWorld方法之前就為一個Joinpoint

Pointcut:切入點的集合,可以通過織入點語法定義N個切入點加入邏輯處理。

Aspect:切面,指的是切面的類。也就是上面聲明Aspect的邏輯集合

Advise:指的是切面上的邏輯比如@Before、@After

Target:被代理對象.上面的案例指的是UserServiceImpl

Weave:織入
作者:zhang6622056

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *