今天学到了AOP 面向切面编程
在这里学到了两个类 1.MethodBeforeAdice
2.AfterReturningAdvice
implement MethodBeforeAdice的类里面有一个before方法会在执行bean之前执行
package com.zyy.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.invoke.MethodHandleInfo;
import java.lang.reflect.Method;
public class BeforeLog implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("使用了"+this.getClass().getName()+"的"+method.getName()+"方法");
}
}
implement AfterReturningAdvice的类里面会有一个afterReturning()方法一个会在执行bean之后执行
package com.zyy.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("使用了"+this.getClass().getName()+"的"+method.getName()+"方法,结果为"+o);
}
}
要使用这两个方法首先需要导入maven依赖
<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>
这里老师说的是只要导入aspectjweaver就能运行了,但我的不知道为什么还需要导入spring-webmvc
下一步就要在ApplicationText.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"/>
<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>
</beans>
这里只要注册好bean 然后写下<aop: 就可以在aop上面alt+enter来自动导入xmlns:aop=“http://www.springframework.org/schema/aop” 接下来就只需要在xsi:schemaLocation=里面加上两个地址http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd 这俩个地址也不用记,只需要将原来的两个地址的beans改为aop就可以了
关于这段代码的理解
<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>
这里面的pointcut代表切入点 后面的expression里面写的execution() 这里按返回值 类名(参数类型)填入
这里的advisor环绕通知 后面的两个参数分别是bean的ID 切入点
测试
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();
}
}