ITeye博客

注解方式AOP应用例子

gu6866 · 2012-05-27 01:15 · Java · Spring · 编程语言 · Markdown

  1. Service接口:
  2. public interface PersonService {
  3. public void save(String name);
  4. public void update(String name, Integer id);
  5. public String getName(Integer id);
  6. }
  7. 接口实现:
  8. public class PersonServiceimp implements PersonService{
  9. @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 plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. 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. ">
  13. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  14. <bean id="aop" class="snail.service.Myintercepter"></bean>
  15. <bean id="pp" class="snail.serviceimp.PersonServiceimp"></bean>
  16. </beans>

 

 

 

 

测试类:

  1. public class test {
  2. public static void main(String[] args) {
  3. ApplicationContextatx=new ClassPathXmlApplicationContext("applicationContext.xml");
  4. PersonService b=(PersonService) atx.getBean("pp");
  5. //Myintercepter ss=(Myintercepter)atx.getBean("aop");
  6. b.save("保存snail");
  7. b.getName(1);
  8. b.update("gengxin", 5);
  9. }
  10. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。