【spring框架】AOP的XML实现(必须掌握)

前端之家收集整理的这篇文章主要介绍了【spring框架】AOP的XML实现(必须掌握)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
spring的AOP可以用Annotation来实现,同样的,也可以通过XML的配置来实现,下面来介绍使用XML配置的过程。

1.声明一个切面
有了schema的支持,切面就和常规的Java对象一样被定义成application context中的一个bean。   对象的字段和方法提供了状态和行为信息,XML文件则提供了切入点和通知信息。
 
切面使用<aop:aspect>来声明,backing bean(支持bean)通过 ref 属性来引用:
  1. <aop:config>
  2. <aop:aspect id="myAspect" ref="aBean">
  3. ...
  4. </aop:aspect>
  5. </aop:config>
  6.  
  7. <bean id="aBean" class="...">
  8. ...
  9. </bean>
切面的支持bean(上例中的"aBean")可以象其他Spring bean一样被容器管理配置以及依赖注入。

2. 声明一个切入点
一个命名切入点可以在<aop:config>元素中定义,这样多个切面和通知就可以共享该切入点。
  1. <aop:config>
  2. <aop:aspect id="myAspect" ref="aBean">
  3. <aop:pointcut id="businessService"
  4. expression="execution(* com.xyz.myapp.service.*.*(..)) && this(service)"/>
  5. <aop:before pointcut-ref="businessService" method="monitor"/>
  6. ...
  7.  
  8. </aop:aspect>
  9. </aop:config>
里面的aop的pointcut还可以这样写:
  1. <aop:aspect id="beforeExample" ref="aBean">
  2. <aop:before
  3. pointcut="execution(* com.xyz.myapp.dao.*.*(..))"
  4. method="doAccessCheck"/>
  5. ...
  6. </aop:aspect>

用项目做实验测试:
之前的Annotation:
LogInterceptor.java:
  1. package cn.edu.hpu.aop;
  2.  
  3.  
  4. import org.aspectj.lang.ProceedingJoinPoint;
  5. import org.aspectj.lang.annotation.AfterReturning;
  6. import org.aspectj.lang.annotation.AfterThrowing;
  7. import org.aspectj.lang.annotation.Around;
  8. import org.aspectj.lang.annotation.Aspect;
  9. import org.aspectj.lang.annotation.Before;
  10. import org.aspectj.lang.annotation.Pointcut;
  11. import org.springframework.stereotype.Component;
  12.  
  13.  
  14. @Aspect
  15. @Component
  16. public class LogInterceptor {
  17. @Pointcut("execution(public * cn.edu.hpu.service..*.add(..))")
  18. public void myMethod(){}
  19. @Before("myMethod()")
  20. public void before(){
  21. System.out.println("method start");
  22. }
  23. @AfterReturning("myMethod()")
  24. public void afterReturning(){
  25. System.out.println("method after ruturning");
  26. }
  27. @AfterThrowing("myMethod()")
  28. public void afterThrowing(){
  29. System.out.println("method after throwing");
  30. }
  31. @Around("myMethod()")
  32. public void AroundMtethod(ProceedingJoinPoint pjp) throws Throwable{
  33. System.out.println("method around start");
  34. pjp.proceed();
  35. System.out.println("method around end");
  36. }
  37. }

将注解全部去掉(除了@Component),改为XML配置(将aop:aspectj注释掉):
  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-2.5.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  12. <context:annotation-config/>
  13. <context:component-scan base-package="cn.edu.hpu"/>
  14. <!-- 可以采用使用aspectj注解的方式产生aop -->
  15. <!-- <aop:aspectj-autoproxy/> -->
  16. <bean id="logInterceptor" class="cn.edu.hpu.aop.LogInterceptor"></bean>
  17. <aop:config>
  18. <aop:pointcut expression="execution(public * cn.edu.hpu.service..*.add(..))" id="servicePointCut"/>
  19. <!-- ref使我们的切面类对象 -->
  20. <aop:aspect id="logAspect" ref="logInterceptor">
  21. <aop:before method="before" pointcut-ref="servicePointCut"/>
  22. <aop:after-returning method="afterReturning" pointcut-ref="servicePointCut"/>
  23. <aop:around method="AroundMtethod" pointcut-ref="servicePointCut"/>
  24. <aop:after-throwing method="afterThrowing" pointcut-ref="servicePointCut"/>
  25. </aop:aspect>
  26. </aop:config>
  27. </beans>


测试:
  1. package cn.edu.hpu.service;
  2. import org.junit.Test;
  3. import org.springframework.beans.factory.beanfactory;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6.  
  7.  
  8. import cn.edu.hpu.dao.UserDao;
  9. import cn.edu.hpu.model.User;
  10.  
  11.  
  12. public class UserServiceTest {
  13. @Test
  14. public void testAdd() throws Exception{
  15. ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
  16. UserService userService=(UserService)ctx.getBean("userService");
  17. System.out.println(userService.getClass());
  18. User u=new User();
  19. u.setUsername("u1");
  20. u.setPassword("p1");
  21. userService.add(u);
  22. ctx.destroy();
  23. }
  24. }
测试结果:
class cn.edu.hpu.service.UserService$ $ EnhancerByCGLIB $$bfd74d3a
method start
method around start
add success!!
method after ruturning
method around end

说明使用XML做AOP代理测试是成功的

XMl的方式是我们以后经常会使用的,原因是,这个切面逻辑如果不是我们自己写的,是使用的别人的,这个切面逻辑是第三方或spring的逻辑,这个时候没办法往源码上加注解,这个时候只能使用XML的方式添加AOP代理。

总结:

Spring有些地方使用Annotation很方便,比如IOC,有些地方使用XML很方便,比如AOP,这主要取决于在实际参加工作之后什么样的情形多一些,什么样的情形少一些。

转载请注明出处:http://www.jb51.cc/article/p-crlrikxd-bay.html

猜你在找的XML相关文章