第一種配置方法:使用@AspectJ标簽
- 在配置檔案中添加<aop:aspectj-autoproxy/>注解
- 建立一個Java檔案,使用@Aspect注解修飾該類
- 建立一個方法,使用@Before、@After、@Around等進行修飾,在注解中寫上切入點的表達式
說明:上述Java檔案建立好後,需要将其在Spring的容器中進行聲明,可以在配置檔案中定義<bean/>節點,也可以使用@Component元件進行修飾
示例:
Java代碼

- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.AfterThrowing;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.springframework.stereotype.Component;
- @Component
- @Aspect
- public class AopLog {
- //方法執行前調用
- @Before("execution (* com.services.impl.*.*(..))")
- public void before() {
- System.out.println("before");
- }
- //方法執行後調用
- @After("execution (* com.services.impl.*.*(..))")
- public void after() {
- System.out.println("after");
- }
- //方法執行的前後調用
- @Around("execution (* com.services.impl.*.*(..))")
- public Object around(ProceedingJoinPoint point) throws Throwable{
- System.out.println("begin around");
- Object object = point.proceed();
- System.out.println("end around");
- return object;
- }
- //方法運作出現異常時調用
- @AfterThrowing(pointcut = "execution (* com.services.impl.*.*(..))",throwing = "ex")
- public void afterThrowing(Exception ex){
- System.out.println("afterThrowing");
- System.out.println(ex);
- }
- }
上面這段代碼中多次使用了重複的切入點,這種情況下,可以使用@Pointcut标注,來修改一個切入點方法(這個方法不需要參數和方法體),然後就可以在@Before等标注中引用該方法作為切入點,示例如下:
Java代碼

- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.aspectj.lang.annotation.Pointcut;
- import org.springframework.stereotype.Component;
- @Component
- @Aspect
- public class AopLog {
- @Pointcut("execution (* com.services.impl.*.*(..))")
- public void pointcut(){}
- //方法執行前調用
- @Before("pointcut()")
- public void before() {
- System.out.println("before");
- }
- //方法執行的前後調用
- @Around("pointcut()")
- public Object around(ProceedingJoinPoint point) throws Throwable{
- System.out.println("begin around");
- Object object = point.proceed();
- System.out.println("end around");
- return object;
- }
- }
第二種配置方法:基于配置檔案的配置
- 建立一個Java檔案,并指定一個用于執行攔截的方法,該方法可以有0個或多個參數
- 在Spring配置檔案中注冊該Java類為一個Bean
- 使用<aop:config/>、<aop:aspect/>等标簽進行配置
示例:
Java檔案
Java代碼

- import org.aspectj.lang.ProceedingJoinPoint;
- public class AopLog {
- //方法執行的前後調用
- public Object runOnAround(ProceedingJoinPoint point) throws Throwable{
- System.out.println("begin around");
- Object object = point.proceed();
- System.out.println("end around");
- return object;
- }
- }
Spring配置檔案
Xml代碼

- <bean id="aopLog" class="com.aop.AopLog"></bean>
- <aop:config>
- <aop:aspect ref="aopLog">
- <aop:around method="runOnAround" pointcut="execution (* com.services.impl.*.*(..))"/>
- </aop:aspect>
- </aop:config>
注意:上面這個示例使用的是around方式的攔截,該方法要求Java類中的方法有一個ProceedingJoinPoint類型的參數
使用第二種方式的AOP配置,在Eclipse(有SpringIDE插件)中被攔截到的方法中有辨別顯示
以上配置基于Spring 3.0.5 進行設定,參考其《Reference Documentation》
最後加一句:spring3的AOP注釋支援&&,||,!等符号,很友善,例如:
@Before("execution(* com.web.dwr..*.*(..)) && !execution(* com.web.dwr.BaseDwr.*(..))")