今天学到了面向切面编程的第三种方式 注解
导入依赖的步骤以及ApplicationContext.xml的步骤和上两种种方法一致(下想想这都是废话了,都是面向切面编程用aop的导入的依赖肯定一样啊,我真傻)
依赖如下
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
ApplicationContext.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">
<bean id="beforeLog" class="com.zyy.log.BeforeLog"/>
<bean id="afterLog" class="com.zyy.log.AfterLog"/>
<bean id="userServiceImpl" class="com.zyy.service.UserServiceImpl"/>
<bean id="diy" class="com.zyy.log.Log"/>
<bean id="autoLog" class="com.zyy.log.AutoLog"/>
<!-- //第一种方式-->
<!-- <aop:config>-->
<!-- <aop:pointcut id="pointcut" expression="execution(* com.zyy.service.UserServiceImpl.*(..))"/>-->
<!-- <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>-->
<!-- <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>-->
<!-- </aop:config>-->
<!-- //第二种方式-->
<!-- <aop:config>-->
<!-- <aop:aspect ref="diy">-->
<!-- <aop:pointcut id="point" expression="execution(* com.zyy.service.UserServiceImpl.*(..))"/>-->
<!-- <aop:after method="frist" pointcut-ref="point"/>-->
<!-- <aop:before method="last" pointcut-ref="point"/>-->
<!-- </aop:aspect>-->
<!-- </aop:config>-->
<!--开启注解支持-->
<aop:aspectj-autoproxy/>
</beans>
和之前一样将上面两种方法都注释掉方便对比学习
对于这里代码的理解
<aop:aspectj-autoproxy/>
有啥好理解的就是开启注解支持 记住就完事了
用注解和第二种方法一样需要创建一个自定义切面类
package com.zyy.log;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AutoLog {
@Before("execution(* com.zyy.service.UserServiceImpl.*(..))")
public void frist(){
System.out.println("这是第一个方法");
}
@After("execution(* com.zyy.service.UserServiceImpl.*(..))")
public void last(){
System.out.println("这是最后一个方法");
}
}
这两个注解都很好懂
测试
import com.zyy.service.USerService;
import com.zyy.service.UserServiceImpl;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) throws BeansException {
ApplicationContext Context = new ClassPathXmlApplicationContext("applicationContext.xml");
USerService userService = Context.getBean("userServiceImpl", USerService.class);
userService.update();
}
}
总的来说今天学到的三种面向切面编程的方式里面,第一种(弄两个实现了MethodBeforeAdvice和AfterReturningAdvice接口的类)功能最全面,里面的method参数啥的可以利用反射获取各种数据,但代码量比较多。第三种(注解)最为简单,但不利于维护也不适合较为复杂的情况。中国人喜欢折中原则所以我选择第二种(doge),主要是第二种代码量一般,同时利于维护,也能满足平时一般的需求,推荐使用