一、简介

AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

二、执行原理图

Aop执行流程.png

三、SpringAOP的本质总结

我们将要进行功能扩展相关的材料以及对应的组织规则告诉Spring容器,Spring容器帮我们动态创建一个代理对象。我们直接从Spring容器中获取代理对象完成功能开发。

四、Schema-based

  1. package com.bjsxt.advice;
  2. import org.springframework.aop.AfterReturningAdvice;
  3. import java.lang.reflect.Method;
  4. public class AfterAdvice implements AfterReturningAdvice {
  5. @Override
  6. public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
  7. //在这个方法中进行代码扩展即可
  8. System.out.println("---------->我是后置通知");
  9. }
  10. }
  11. package com.bjsxt.advice;
  12. import org.springframework.aop.AfterReturningAdvice;
  13. import java.lang.reflect.Method;
  14. public class AfterAdvice implements AfterReturningAdvice {
  15. @Override
  16. public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
  17. //在这个方法中进行代码扩展即可
  18. System.out.println("---------->我是后置通知");
  19. }
  20. }
  21. package com.bjsxt.advice;
  22. import org.aopalliance.intercept.MethodInterceptor;
  23. import org.aopalliance.intercept.MethodInvocation;
  24. public class RoundAdvice implements MethodInterceptor {
  25. @Override
  26. public Object invoke(MethodInvocation methodInvocation) throws Throwable {
  27. System.out.println("环绕之前");
  28. //放行
  29. Object proceed = methodInvocation.proceed();
  30. System.out.println("环绕之后");
  31. return proceed;
  32. }
  33. }
  34. package com.bjsxt.advice;
  35. import org.springframework.aop.ThrowsAdvice;
  36. public class ThrowAdvice implements ThrowsAdvice {
  37. public void afterThrowing(Exception ex) throws Throwable{
  38. System.out.println("我是异常通知");
  39. }
  40. }

applicationContext.xml

  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" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  6. <bean id="userService" class="com.bjsxt.service.impl.UserServiceImpl"></bean>
  7. <!--前置通知对象-->
  8. <bean id="beforeAdvice" class="com.bjsxt.advice.BeforeAdvice"></bean>
  9. <!--后置通知对象-->
  10. <bean id="afterAdvice" class="com.bjsxt.advice.AfterAdvice"></bean>
  11. <!--环绕通知对象-->
  12. <bean id="roundAdvice" class="com.bjsxt.advice.RoundAdvice"></bean>
  13. <!--异常通知对象-->
  14. <bean id="throwAdvice" class="com.bjsxt.advice.ThrowAdvice"></bean>
  15. <!--织入横切面-->
  16. <aop:config>
  17. <!--指定切点-->
  18. <aop:pointcut id="pt1" expression="execution(* com.bjsxt.service.impl.UserServiceImpl.bb())"/>
  19. <!--给指定切点追加通知-->
  20. <aop:advisor advice-ref="beforeAdvice" pointcut-ref="pt1"></aop:advisor>
  21. <aop:advisor advice-ref="afterAdvice" pointcut-ref="pt1"></aop:advisor>
  22. <aop:advisor advice-ref="roundAdvice" pointcut-ref="pt1"></aop:advisor>
  23. <aop:advisor advice-ref="throwAdvice" pointcut-ref="pt1"></aop:advisor>
  24. </aop:config>
  25. </beans>

五、Aspectj

AspectJAdvice

  1. package com.bjsxt.advice;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. public class AspectJAdvice {
  4. //前置通知
  5. public void before(){
  6. System.out.println("-----前置通知-----");
  7. }
  8. //前置通知
  9. public void after(){
  10. System.out.println("-----后置通知-----");
  11. }
  12. //前置通知
  13. public Object around(ProceedingJoinPoint point) throws Throwable {
  14. System.out.println("-----环绕通知前-----");
  15. Object proceed = point.proceed();
  16. System.out.println("-----环绕通知后-----");
  17. return proceed;
  18. }
  19. //前置通知
  20. public void throwsAdvice(){
  21. System.out.println("-----异常通知-----");
  22. }
  23. }

applicationContext.xml

  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" xmlns:aop="http://www.springframework.org/schema/aop"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
  6. <bean id="userService" class="com.bjsxt.service.impl.UserServiceImpl"></bean>
  7. <!--配置通知类对象-->
  8. <bean id="aspect" class="com.bjsxt.advice.AspectJAdvice"></bean>
  9. <aop:config>
  10. <aop:aspect ref="aspect">
  11. <aop:pointcut id="pt1" expression="execution(* com.bjsxt.service.impl.UserServiceImpl.bb())"/>
  12. <!--前置通知-->
  13. <aop:before method="before" pointcut-ref="pt1"></aop:before>
  14. <!--后置通知-->
  15. <aop:after method="after" pointcut-ref="pt1"></aop:after>
  16. <!--环绕通知-->
  17. <aop:around method="around" pointcut-ref="pt1"></aop:around>
  18. <!--异常通知-->
  19. <aop:after-throwing method="throwsAdvice" pointcut-ref="pt1"></aop:after-throwing>
  20. </aop:aspect>
  21. </aop:config>
  22. </beans>

@Aspectj在扫描时与Aspectj顺序不一样
@Aspectj:环绕在外,前后置通知在内
Aspectj:环绕在内,前后置通知在外

注解方式

  1. package com.bjsxt.advice;
  2. import org.aspectj.lang.annotation.After;
  3. import org.aspectj.lang.annotation.Aspect;
  4. import org.aspectj.lang.annotation.Pointcut;
  5. import org.springframework.stereotype.Component;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. @Component
  9. @Aspect
  10. public class AspectJAdvice {
  11. //切点
  12. @Pointcut("execution(* com.bjsxt.service.impl.AccountServiceImpl.zq(..))")
  13. public void pt(){}
  14. //后置通知
  15. @After("pt()")
  16. public void after(){
  17. Date date = new Date();
  18. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  19. String format = dateFormat.format(date);
  20. System.out.println("用户在"+format+"完成了转账操作");
  21. }
  22. }
  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. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. https://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. https://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/aop
  12. https://www.springframework.org/schema/aop/spring-aop.xsd
  13. http://www.springframework.org/schema/tx
  14. https://www.springframework.org/schema/tx/spring-tx.xsd">
  15. <!--IOC+DI扫描-->
  16. <context:component-scan base-package="com.bjsxt.service,com.bjsxt.pojo,com.bjsxt.advice"></context:component-scan>
  17. <!--加载扫描AOP中注解驱动-->
  18. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  19. <!--事务注解驱动-->
  20. <tx:annotation-driven></tx:annotation-driven>
  21. <!--引入属性文件-->
  22. <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
  23. <!--[1] 建立数据库连接-->
  24. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  25. <property name="driverClassName" value="${m_driver}"></property>
  26. <property name="url" value="${m_url}"></property>
  27. <property name="username" value="${m_username}"></property>
  28. <property name="password" value="${m_password}"></property>
  29. </bean>
  30. <!--[2] 获取sqlsessionfactory对象-->
  31. <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
  32. <property name="dataSource" ref="dataSource"></property>
  33. <property name="typeAliasesPackage" value="com.bjsxt.pojo"></property>
  34. </bean>
  35. <!--[3] 扫描mapper文件-->
  36. <bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  37. <property name="sqlSessionFactoryBeanName" value="factory"></property>
  38. <property name="basePackage" value="com.bjsxt.mapper"></property>
  39. <!--自动创建了Peoplemapper Bean对象-->
  40. </bean>
  41. <!--声明式事务的配置-->
  42. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  43. <property name="dataSource" ref="dataSource"></property>
  44. </bean>
  45. </beans>