---
title: "spring使用动态代理面向切面编程(AOP) annotation"
author: "cdxs2"
date: "2013-09-14T13:31:25Z"
tags: []
categories: ["编程语言"]
slug: "spring-aop-annotation"
draft: false
lastmod: "2013-09-14T17:44:24Z"
id: 1942198
url: "https://cdxs2.iteye.com/blog/1942198"
---

在使用注解的时候，首先得在配置文件bean.xml中添加命名空间：

```
xmlns:aop="http://www.springframework.org/schema/aop"
```

 

然后在xsi:schemaLocation中添加：

```
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
```

 

再次引入：

```
<aop:aspectj-autoproxy/>
```

 

bean.xml

```
<?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.test"></context:component-scan>
	<!-- 让spring容器自动生成代理 -->
	<aop:aspectj-autoproxy/>
	
</beans>
```

 

MyInterceptor.java

```
package com.test.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyInterceptor {
	// 切入点语法
	@Before("execution(public void com.test.dao.impl.UserDAOImpl.save(com.test.model.User))")
	public void beforeMethod() {
		System.out.println("start mothod!");
	}
}
```

 

另一种写法

MyInterceptor2.java

```
package com.test.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyInterceptor2 {
	/*
	// 表示任何返回类型的com.test.dao以及其子包下的任何方法
	@Before("execution(public * com.test.dao..*.*(..))")
	public void beforeMethod() {
		System.out.println("MyInterceptor2 before mothod!");
	}
	
	@AfterReturning("execution(public * com.test.dao..*.*(..))")
	public void afterReturningMethod() {
		System.out.println("MyInterceptor2 afterReturning mothod!");
	}
	*/
	
	//以上重复编写"execution(public * com.test.dao..*.*(..))"，用以下可以进行简化
	
	@Pointcut("execution(public * com.test.dao..*.*(..))")
	public void myMethod(){}
	
	@Before("myMethod()")
	public void beforeMethod() {
		System.out.println("MyInterceptor2 before mothod!");
	}
	
	@AfterReturning("myMethod()")
	public void afterReturningMethod() {
		System.out.println("MyInterceptor2 afterReturning mothod!");
	}
	
	@AfterThrowing("myMethod()")
	public void afterThrowingMethod() {
		System.out.println("MyInterceptor2 afterThrowing mothod!");
	}
	
	@Around("myMethod()")
	public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("MyInterceptor2 around start mothod!");
		pjp.proceed();
		System.out.println("MyInterceptor2 around end mothod!");
	}
	
}
```

 

往没有实现接口的类的方法做切入点

MyInterceptor3.java

```
package com.test.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyInterceptor3 {
	// 这里往service里面的方法做切入点，运行会报错，从而产生不了代理，需要引入cglib包
	// 为什么？
	// 原因：service包里面的类没有实现接口，一个类实现了接口，它就会用JDK自带的Proxy和InvocationHandler来帮你产生代理，
	// 若类没有实现接口，它会直接操作二进制码的这种类库，也就是cglib来帮你产生代理，所以这里需要引入cglib包
	@Pointcut("execution(public * com.test.service..*.add(..))")
	public void myMethod(){}
	
	@Before("myMethod()")
	public void beforeMethod() {
		System.out.println("MyInterceptor3 before mothod!");
	}
	
	@AfterReturning("myMethod()")
	public void afterReturningMethod() {
		System.out.println("MyInterceptor3 afterReturning mothod!");
	}
	
	@AfterThrowing("myMethod()")
	public void afterThrowingMethod() {
		System.out.println("MyInterceptor3 afterThrowing mothod!");
	}
	
	@Around("myMethod()")
	public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("MyInterceptor3 around start mothod!");
		pjp.proceed();
		System.out.println("MyInterceptor3 around end mothod!");
	}
	
}
```

 

其他内容和“spring中Resource和Component注解”这一节一致