1.声明一个切面
有了schema的支持,切面就和常规的Java对象一样被定义成application context中的一个bean。 对象的字段和方法提供了状态和行为信息,XML文件则提供了切入点和通知信息。
切面使用<aop:aspect>来声明,backing bean(支持bean)通过 ref 属性来引用:
切面的支持bean(上例中的"aBean")可以象其他Spring bean一样被容器管理配置以及依赖注入。
- <aop:config>
- <aop:aspect id="myAspect" ref="aBean">
- ...
- </aop:aspect>
- </aop:config>
- <bean id="aBean" class="...">
- ...
- </bean>
2. 声明一个切入点
一个命名切入点可以在<aop:config>元素中定义,这样多个切面和通知就可以共享该切入点。
里面的aop的pointcut还可以这样写:
- <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: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
- @Component
- public 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 {
- @Test
- public 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,这主要取决于在实际参加工作之后什么样的情形多一些,什么样的情形少一些。