---
title: "注解方式AOP应用例子"
author: "gu6866"
date: "2012-05-27T01:15:34Z"
tags: []
categories: ["编程语言"]
slug: "aop"
draft: false
lastmod: "2012-05-27T01:15:34Z"
id: 1542126
url: "https://premier9527.iteye.com/blog/1542126"
---

01. Service接口：
02. public interface PersonService {
03. public void save(String name);
04. public void update(String name, Integer id);
05. public String getName(Integer id);
06. }
07. 接口实现：
08. public class PersonServiceimp implements PersonService{
09. @Override
10. public void save(String name) {
11. System.out.println("woshi save()");
12. }
13. @Override
14. public void update(String name, Integer id) {
15. System.out.println("woshi update()");
16. }
17. @Override
18. public String getName(Integer id) {
19. System.out.println("woshi getName()");
20. return id+"";
21. }
22. }
23. 切面类：
24. @Aspect
25. public class Myintercepter {
26. @Pointcut("execution (* snail.serviceimp.PersonServiceimp.\*(..))")
27. public void anyMethod(){}
28. @Before("anyMethod() && args(userName)")//定义前置通知,anyMethod()是必须的方法，//args（username）不是必须的，是接收方法的参数
29. public void doAccessCheck(String userName) {
30. System.out.println("前置通知"+userName);
31. }
32. @AfterReturning(pointcut="anyMethod()",returning="revalue")//定义后置通知 returning参数是接收被拦截的方法的返回值
33. public void doReturnCheck(String revalue) {
34. System.out.println("后置通知"+revalue);
35. }
36. @AfterThrowing(pointcut="anyMethod()", throwing="ex")//定义例外通知
37. public void doExceptionAction(Exception ex) {
38. System.out.println("例外通知"+ex);
39. }
40. @After("anyMethod()")//定义最终通知
41. public void doReleaseAction() {
42. System.out.println("最终通知");
43. }
44. @Around("anyMethod()")//环绕通知
45. public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
46. System.out.println("环绕通知");
47. return pjp.proceed();
48. }
49. }

 

 

 

 

 

xml配置文件

 

**\[xhtml]** [view plain](http://blog.csdn.net/snailandjava/article/details/6431564 "view plain")[copy](http://blog.csdn.net/snailandjava/article/details/6431564 "copy")

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xmlns:context="http://www.springframework.org/schema/context"
05. xmlns:aop="http://www.springframework.org/schema/aop"
06. xsi:schemaLocation="http://www.springframework.org/schema/beans
07. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
08. http://www.springframework.org/schema/context
09. http://www.springframework.org/schema/context/spring-context-3.0.xsd
10. http://www.springframework.org/schema/aop
11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
12. "&gt;
13. &lt;aop:aspectj-autoproxy&gt;&lt;/aop:aspectj-autoproxy&gt;
14. &lt;bean id="aop" class="snail.service.Myintercepter"&gt;&lt;/bean&gt;
15. &lt;bean id="pp" class="snail.serviceimp.PersonServiceimp"&gt;&lt;/bean&gt;
16. &lt;/beans&gt;

 

 

 

 

测试类：

01. public class test {
02. public static void main(String\[] args) {
03. ApplicationContextatx=new ClassPathXmlApplicationContext("applicationContext.xml");
04. PersonService b=(PersonService) atx.getBean("pp");
05. //Myintercepter ss=(Myintercepter)atx.getBean("aop");
06. b.save("保存snail");
07. b.getName(1);
08. b.update("gengxin", 5);
09. }
10. }