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

来源:互联网 发布:linux jdk版本降级 编辑:程序博客网 时间:2024/05/23 11:34
spring的AOP可以用Annotation来实现,同样的,也可以通过XML的配置来实现,下面来介绍使用XML配置的过程。

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

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

用项目做实验测试:
之前的Annotation:
LogInterceptor.java:
package cn.edu.hpu.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@Componentpublic class LogInterceptor {@Pointcut("execution(public * cn.edu.hpu.service..*.add(..))")public void myMethod(){}@Before("myMethod()")public void before(){System.out.println("method start");} @AfterReturning("myMethod()")public void afterReturning(){System.out.println("method after ruturning");} @AfterThrowing("myMethod()")public void afterThrowing(){System.out.println("method after throwing");} @Around("myMethod()")public void AroundMtethod(ProceedingJoinPoint pjp) throws Throwable{System.out.println("method around start");pjp.proceed();System.out.println("method around end");} }

将注解全部去掉(除了@Component),改为XML配置(将aop:aspectj注释掉):
<?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:annotation-config/>  <context:component-scan base-package="cn.edu.hpu"/>  <!-- 可以采用使用aspectj注解的方式产生aop -->  <!-- <aop:aspectj-autoproxy/> -->    <bean id="logInterceptor" class="cn.edu.hpu.aop.LogInterceptor"></bean>  <aop:config>  <aop:pointcut expression="execution(public * cn.edu.hpu.service..*.add(..))" id="servicePointCut"/>  <!-- ref使我们的切面类对象 -->  <aop:aspect id="logAspect" ref="logInterceptor">  <aop:before method="before" pointcut-ref="servicePointCut"/>  <aop:after-returning method="afterReturning" pointcut-ref="servicePointCut"/>  <aop:around method="AroundMtethod" pointcut-ref="servicePointCut"/>  <aop:after-throwing method="afterThrowing" pointcut-ref="servicePointCut"/>  </aop:aspect>  </aop:config></beans>


测试:
package cn.edu.hpu.service;import org.junit.Test;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.edu.hpu.dao.UserDao;import cn.edu.hpu.model.User;public class UserServiceTest {@Testpublic void testAdd() throws Exception{ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");UserService userService=(UserService)ctx.getBean("userService");System.out.println(userService.getClass());User u=new User();u.setUsername("u1");u.setPassword("p1");userService.add(u);ctx.destroy();}}
测试结果:
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://blog.csdn.net/acmman/article/details/44516883

0 0