方式一:使用原生SPring API接口
个人理解:aop是一种变成思想,再不影响业务/代码的情况下,实现动态的增强
切入点:把某个类放到pointcut的切入点里
切面:多了个aspect方法 <aop:aspect ref=“diypoint”>,然后ref后面写的是需要切面的类
1.AOP的使用,导入织入包
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
<scope>runtime</scope>
</dependency>
1.准备环境:
这是log输出。。。调用method方法,图片下面写错了
2.使用spring API接口
测试:
方式一:
<!-- 方式一:使用原生SPring API接口-->
<!-- <aop:config>-->
<!-- <!– 切入点 expression:表达式execution(要执行的位置* * * * *)–>-->
<!-- <aop:pointcut id="pointcut" expression="execution(* com.lin.service.UserServiceImpl.*(..))" />-->
<!--<!– 执行环绕增强–>-->
<!--<!– 把log这类切入到pointcut里–>-->
<!-- <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>-->
<!-- <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>-->
<!-- </aop:config>-->
方法二:自定义来实现aop
<!-- 方式二-->
<bean id="diypoint" class="com.lin.diy.DiyPointCut"/>
<aop:config>
<!-- 自定义切面;ref要引用的类-->
<aop:aspect ref="diypoint">
<!-- 切入点-->
<aop:pointcut id="point" expression="execution(* com.lin.service.UserServiceImpl.*(..))"/>
<!-- 通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
方式三:使用注解开发
需要导入jar包:maven
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
</dependency>
下面这个垃圾方法,看一眼就好~~~~~~~~~~