业务类
package oyb.service;public interface IUserService {public void add();public void delete(); }
package oyb.service;public class UserServiceImpl implements IUserService {@Overridepublic void add() {System.out.println("添加用户。。。。");}@Overridepublic void delete() {System.out.println("删除用户");} }
切面类
package oyb.aspect;import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation;public class MyAspect implements MethodInterceptor{@Overridepublic Object invoke(MethodInvocation methodInvocation) throws Throwable {return methodInvocation.proceed();} }
Spring配置文件
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--配置UserService--><bean id="userService" class="oyb.service.UserServiceImpl"></bean><!--默认情况下Spring的AOP生成的代理是JDK的Proxy实现的--><!--配置切面对象--><bean id="myAspect" class="oyb.aspect.MyAspect"></bean><!--proxy-target-class为true表示开启cglib增强--><aop:config proxy-target-class="true"><aop:pointcut id="myPointcut" expression="execution(* oyb.service.*.*(..))"></aop:pointcut><!--通知 关联 切入点--><aop:advisor advice-ref="myAspect" pointcut-ref="myPointcut"></aop:advisor></aop:config></beans>
package oyb.test;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import oyb.service.IUserService;public class test {@Testpublic void test(){ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");IUserService userService = (IUserService) context.getBean("userService");userService.add();} }
测试结果:
之前一直抛出Bean named 'myAspect' must be of type [org.aopalliance.aop.Advice],这个异常,检查之后发现切面类MyAspect中没有实现MethodInterceptor接口,所以会报这个错误。
这个简单的案例当中,导入了com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar这个包
这里简单介绍一下AspectJ,AspectJ是一个基于Java语言的AOP框架,在Spring 2.0以后新增了对AspectJ切点表达式支持,在新版本的Spring当中,大部分都使用AspectJ的方式来进行开发。
下面介绍一下AspectJ的几种通知类型
1.before:前置通知,主要应用到各种校验。在方法执行前执行,如果通知抛出异常,方法将进行不了。
2.afterReturning:后置通知,主要应用于常规数据处理。方法执行如果抛出异常,则也执行不了。在方法执行后执行,可以获得方法的返回值。
3.around:环绕通知,
4.afterThrowing:抛出异常通知,方法抛出异常时执行,否则无法执行
5.after:最终通知,方法执行完毕之后执行,无论是否抛出异常
例子:
1.导入相应的包(基于xml配置):
2.业务类
package oyb.service;public interface IUserService {public void add();public void delete();public void update(); }
package oyb.service.impl;import oyb.service.IUserService;public class UserServiceImpl implements IUserService {@Overridepublic void add() {System.out.println("添加用户。。。");}@Overridepublic void delete() {System.out.println("删除用户。。。");}@Overridepublic void update() {System.out.println("更新用户。。。");} }
3.切面类
package oyb.aspect;import org.aspectj.lang.JoinPoint;public class MyAspect {public void myBefore(JoinPoint joinPoint){System.out.println("前置通知");}public void myReturning(JoinPoint joinPoint){System.out.println("后置通知");} }
配置文件
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="userService" class="oyb.service.impl.UserServiceImpl"></bean><bean id="myAspect" class="oyb.aspect.MyAspect"></bean><aop:config><aop:aspect ref="myAspect"><aop:pointcut id="myPointcut" expression="execution(* oyb.service.impl.UserServiceImpl.*(..))"/><!--前置通知--><aop:before method="myBefore" pointcut-ref="myPointcut"></aop:before><!--后置通知--><aop:after-returning method="myReturning" pointcut-ref="myPointcut" ></aop:after-returning></aop:aspect></aop:config></beans>
测试类
package oyb.test;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import oyb.service.IUserService;public class test {@Testpublic void test(){ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");IUserService userService = (IUserService) context.getBean("userService");userService.add();} }
测试结果
使用注解
Spring配置文件中
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><context:component-scan base-package="oyb"></context:component-scan><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>
业务类中添加注解如图
切面类
package oyb.aspect;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component;@Component @Aspect public class MyAspect {//声明公共切入点@Pointcut("execution(* oyb.service.impl.UserServiceImpl.*(..))")public void myPointcut(){}@Before(value ="myPointcut()")public void myBefore(JoinPoint joinPoint){System.out.println("前置通知");}@AfterReturning(value = "myPointcut()")public void myReturning(JoinPoint joinPoint){System.out.println("后置通知");} }
测试类
package oyb.test;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import oyb.service.IUserService;public class test {@Testpublic void test(){ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");IUserService userService = (IUserService) context.getBean("userService");userService.add();} }
测试结果如图: