SpringAOP之SpringAPI接口实现
第一步创建maven项目导入依赖
<dependencies>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
很简单的三个依赖,Spring依赖导入spring-webmvc后很多spring的包都被导入了,junit依赖单元测试,spring-webmvc植入包,用AOP必须导入这个依赖.
第二步
- 写一个接口和实现该接口的类,这个类就是我们的切入点,也就是我们要AOP的类.在实际开发中也就是我们的业务层.
接口:
public interface Service {
public void add();
public void delete();
public void update();
public void query();
}
实现类:
public class ServiceImpl implements Service{
public void add() {System.out.println("增加方法!");}
public void delete() {System.out.println("删除方法!"); }
public void update() {System.out.println("修改方法!"); }
public void query() { System.out.println("查询方法!"); }
}
- 写我们要环绕的类,就是我们切入之后要执行的方法,类似于一个代理类,要实现Spring的接口.
方法执行之前: MethodBeforeAdvice
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("log"+o.getClass().getName()+"的"+method.getName()+"方法执行之前!");
}
}
方法执行之后:
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("log"+method.getName()+"方法执行之hou!");
}
}
以这两个为例子,点进springframework.aop中可以看到很多方法都可以配置.
第三步配置Spring配置文件AppliactionContext.xml
- 注册bean把三个类都放到IOC容器中
- 配置AOP:execution表达式是表示要找到的类的所有方法,括号中的…表示匹配所有参数类型的方法.
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册Bean-->
<bean id="ServiceImpl" class="com.shao.service.ServiceImpl"/>
<bean id="Log" class="com.shao.log.Log"/>
<bean id="AfterLog" class="com.shao.log.AfterLog"/>
<!--配置AOP-->
<aop:config>
<!--切入点-->
<!--expression: 表达式:execution(要执行的位置 *****)-->
<aop:pointcut id="pointcut" expression="execution(* com.shao.service.ServiceImpl.*(..))"/>
<!--执行环绕增加-->
<aop:advisor advice-ref="Log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="AfterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
第四步测试
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("AppliactionContext.xml");
Service serviceImpl = context.getBean("ServiceImpl", Service.class);
serviceImpl.add();
}
}
可以看到: