将方法切入到业务层
- 搭建service
package com.service.impl; import com.service.LearnService; public class LearnServiceImpl implements LearnService { public void one() { System.out.println("LearnServiceImpl中的one方法执行了"); } public void two(String name) { System.out.println("LearnServiceImpl中的two方法执行了" + name); } public int three() { System.out.println("LearnServiceImpl中的three方法执行了"); return 0; } }
- 搭建切入的方法
package com.service.impl; import com.service.LearnService; public class LearnServiceImpl implements LearnService { public void one() { System.out.println("LearnServiceImpl中的one方法执行了"); } public void two(String name) { System.out.println("LearnServiceImpl中的two方法执行了" + name); } public int three() { System.out.println("LearnServiceImpl中的three方法执行了"); return 0; } }
- 配置bean.xml
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置service --> <bean id="learnService" class="com.service.impl.LearnServiceImpl"></bean> <!-- spring中基于XML的AOP配置步骤 1、把通知Bean也交给spring来管理 2、使用aop:config标签表明开始AOP的配置 3、使用aop:aspect标签表明配置切面 id属性:是给切面提供一个唯一标识 ref属性:是指定通知类bean的Id 4、在aop:aspect标签的内部使用对应标签来配置通知的类型 我们现在示例是让printLog方法在切入点方法执行之前:所以是前置通知 aop:before: 表示配置前置通知 method属性:用于指定Logger类中那个方法是前置通知 pointcut属性:用于制动切入点表达式,该表达式的含义指的是对业务层中那些方法增强 切入表达式的写法: 关键字:execution(表达式) 表达式: 访问修饰符 返回值 包名.包名.包名.....类名.方法名(参数列表) 标准的表达式写法: public void com.service.impl.LearnServiceImpl.one() 返回值可以使用通配符,表示任意返回值 * com.service.impl.LearnServiceImpl.one() 包名可以使用..表示当前及其子包 * *..LearnServiceImpl.one() 类名和方法名都可以使用*来实现通配 * *..*.*() 这样写不会实现方法有参数的 * *..*.*(*) 参数有,可以为任意类型 全通配写法: * *..*.*(..) 实际开发中切入点的表达式的通常写法: * com.service.impl.*.*(..) --> <!-- 配置Logger类 --> <bean id="logger" class="com.logger.Logger"></bean> <!-- 配置Aop --> <aop:config> <!-- 配置切面 --> <aop:aspect id="logAdvice" ref="logger"> <!-- 配置通知的类型,并且建立通知方法和切入点方法的关联 --> <aop:before method="printLog" pointcut="execution( * *..LearnServiceImpl.one())"></aop:before> </aop:aspect> </aop:config> </beans>
- 测试
package com; import com.service.LearnService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml"); LearnService learnService = applicationContext.getBean("learnService", LearnService.class); learnService.one(); } }
- pom配置
git地址:https://gitee.com/Xiaokeworksveryhard/spring_aop.git<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>spring.io</groupId> <artifactId>spring_aop</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.7</version> </dependency> </dependencies> </project>
AOP的四个通知
- 建立四个通知方法
/** * 前置通知 */ private void beforePrintLog(){ System.out.println("Logger类中的printLog方法开始记录日志了...............beforePrintLog"); } /** * 后置通知 */ private void afterPrintLog(){ System.out.println("Logger类中的printLog方法开始记录日志了...............afterPrintLog"); } /** * 异常通知 */ private void afterThrowPintLog(){ System.out.println("Logger类中的printLog方法开始记录日志了...............afterThrowPintLog"); } /** * 最终通知 */ private void finallyPrintLog(){ System.out.println("Logger类中的printLog方法开始记录日志了..............finallyPrintLog."); }
- 配置bean.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--配置service --> <bean id="learnService" class="com.service.impl.LearnServiceImpl"></bean> <!-- 配置Logger类 --> <bean id="logger" class="com.logger.Logger"></bean> <!-- 配置Aop --> <aop:config> <!-- 配置切入点表达式 id属性用于指定表达式的唯一标识。 expression属性用于指定表达式内容 此标签写早aop:aspect标签内部职能当前切面使用。 它还可以卸载aop:aspect外面,此时就变成了所有切面可用。--> <aop:pointcut id="pt1" expression="execution( * com.service.impl.*.*(..))"/> <aop:aspect id="logAdvice" ref="logger"> <!-- 前置通知 --> <aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before> <!-- 后置通知 --> <aop:after-returning method="afterPrintLog" pointcut-ref="pt1"></aop:after-returning> <!-- 异常通知 --> <aop:after-throwing method="afterThrowPintLog" pointcut-ref="pt1"></aop:after-throwing> <!-- 最终通知 --> <aop:after method="finallyPrintLog" pointcut-ref="pt1"></aop:after> <!-- <aop:pointcut id="pt1" expression="execution( * com.service.impl.*.*(..))"/>--> </aop:aspect> </aop:config> </beans>
- git地址:https://gitee.com/Xiaokeworksveryhard/spring_aop_four_notice.git
AOP的环绕通知
- 建立环绕通知方法
/** * 环绕通知 *问题: * 当我们配置了环绕通知,切入点方法没有执行,而通知方法执行了。 * 分析: * 通过对比动态代理中的环绕通知代码,发现动态代理的环绕通知有明确的切入点方法调用,而我们的代码中没有。 * 解决: * Spring框架为我们提供了一个接口,ProceedingJoinPoint. 还借口有一个方法proceed(),此方法就相当于明确调用切入点方法。 * 该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类供我们使用。 * spring中的环绕通知: * 他是spring框架为我们提供的一种可以在代码中手动控制增强法法何时执行的方式。 * */ private Object aroundPrintLog(ProceedingJoinPoint point){ Object reValue = null; try{ System.out.println("前置通知"); Object[] args = point.getArgs(); reValue = point.proceed(args); System.out.println("后置通知"); }catch (Throwable t){ System.out.println("异常通知"); throw new RuntimeException(t); }finally { System.out.println("最终通知"); } return reValue; }
- 配置bean.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--配置service --> <bean id="learnService" class="com.service.impl.LearnServiceImpl"></bean> <!-- 配置Logger类 --> <bean id="logger" class="com.logger.Logger"></bean> <!-- 配置Aop --> <aop:config> <!-- 配置切入点表达式 id属性用于指定表达式的唯一标识。 expression属性用于指定表达式内容 此标签写早aop:aspect标签内部职能当前切面使用。 它还可以卸载aop:aspect外面,此时就变成了所有切面可用。--> <aop:pointcut id="pt1" expression="execution( * com.service.impl.*.*(..))"/> <aop:aspect id="logAdvice" ref="logger"> <!-- 配置环绕通知 --> <aop:around method="aroundPrintLog" pointcut-ref="pt1"></aop:around> </aop:aspect> </aop:config> </beans>
- git地址:https://gitee.com/Xiaokeworksveryhard/spring_aop_four_notice.git
AOP的注解通知
- 改造service
@Service("learnService")替换 <bean id="learnService" class="com.service.impl.LearnServiceImpl">
- 改造注入方法
@Component("logger")替换 <bean id="logger" class="com.logger.Logger"></bean>
- 改造切面
package com.logger; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; /** * 用于记录日志的工具类,它里面提供了公共的代码 */ @Component("logger") @Aspect//表示当前类是一个切面类 public class Logger { @Pointcut("execution( * com.service.impl.*.*(..))") public void pt1(){} /** * 前置通知 */ //@Before("pt1()") private void beforePrintLog(){ System.out.println("Logger类中的printLog方法开始记录日志了...............beforePrintLog"); } /** * 后置通知 */ //@AfterReturning("pt1()") private void afterPrintLog(){ System.out.println("Logger类中的printLog方法开始记录日志了...............afterPrintLog"); } /** * 异常通知 */ //@AfterThrowing("pt1()") private void afterThrowPintLog(){ System.out.println("Logger类中的printLog方法开始记录日志了...............afterThrowPintLog"); } /** * 最终通知 */ //@After("pt1()") private void finallyPrintLog(){ System.out.println("Logger类中的printLog方法开始记录日志了..............finallyPrintLog."); } /** * 环绕通知 *问题: * 当我们配置了环绕通知,切入点方法没有执行,而通知方法执行了。 * 分析: * 通过对比动态代理中的环绕通知代码,发现动态代理的环绕通知有明确的切入点方法调用,而我们的代码中没有。 * 解决: * Spring框架为我们提供了一个接口,ProceedingJoinPoint. 还借口有一个方法proceed(),此方法就相当于明确调用切入点方法。 * 该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类供我们使用。 * spring中的环绕通知: * 他是spring框架为我们提供的一种可以在代码中手动控制增强法法何时执行的方式。 * */ @Around("pt1()") private Object aroundPrintLog(ProceedingJoinPoint point){ Object reValue = null; try{ System.out.println("前置通知"); Object[] args = point.getArgs(); reValue = point.proceed(args); System.out.println("后置通知"); }catch (Throwable t){ System.out.println("异常通知"); throw new RuntimeException(t); }finally { System.out.println("最终通知"); } return reValue; } }
- 改造bean.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!--配置spring 创建容器需要扫描的包--> <context:component-scan base-package="com"></context:component-scan> <!-- 配置spring开启注解Aop的注解 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
- git地址:https://gitee.com/Xiaokeworksveryhard/spring_aop_annotations.git
全注解:
- 创建BeanConfiguration
@Configuration @ComponentScan(basePackages = "com") @EnableAspectJAutoProxy public class BeanConfiguration { }
-
测试
class MainTestTest { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfiguration.class); LearnService learnService = applicationContext.getBean("learnService", LearnService.class); learnService.one(); } }
- git地址:https://gitee.com/Xiaokeworksveryhard/spring_aop_annotations.git
- 总结:注解配置要使用环绕通知手动配置,不能使用四个通知自动配置,因为四个通知顺序错乱