aop 是spring 中一个重要的内容
先聊一聊spring的aop实现切面编程
假如有一天你写了一个服务接口和一个服务类
public interface UserService {
void add();
void delete();
void update();
void query();
}
public class UserServiceImpl implements UserService{
public void add() {
System.out.println("添加一个用户");
}
public void delete() {
System.out.println("删除一个用户");
}
public void update() {
System.out.println("修改一个用户");
}
public void query() {
System.out.println("查询一个用户");
}
}
你在测试类中成功实现了对用户增删查改
有一天你的客户要求添加一个用户开始操作内容的日志和一个操作完成之后的日志,在不改动原有代码的情况下进行增加功能的话就需要使用到aop思想
咱们就可以用下面的方式进行处理
首先添加一个架包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9.1</version>
</dependency>
目前idea2022年使用的版本为1.9.9.1的 没有这个给架包 spring的aop
是无法实现的
然后咱们写一个操作数据之前的日志 log
public class Log implements MethodBeforeAdvice {
//method 要执行的目标对象方法
//args 参数
//target 目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+" "+method.getName()+"执行");
}
}
写一个操作之后的日志 Afterlog
public class AfterLog implements AfterReturningAdvice {
//执行之后的返回值
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(method.getName()+"结果为"+returnValue);
}
}
在到bean.xml 里面进行spring的配置
<?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
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.chen.services.UserServiceImpl"/>
<bean id="log" class="com.chen.log.Log"/>
<bean id="afterLog" class="com.chen.log.AfterLog"/>
<!--配置aop 导入aop约束-->
<aop:config>
<!--切入点 pointcut 表达式execution-->
<aop:pointcut id="pointcut" expression="execution(* com.chen.services.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<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) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
执行结果为:
com.chen.services.UserServiceImpl add执行
添加一个用户
add结果为null
这就可以在不改动原有的代码情况下进行添加需求
再聊聊使用自定义类的方式进行aop的前面编程书写
创建一个类DiyPointCut
public class DiyPointCut {
public void before(){
System.out.println("============== 方法执行前 ==============");
}
public void after(){
System.out.println("============== 方法执行后 ==============");
}
}
再对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
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.chen.services.UserServiceImpl"/>
<bean id="diy" class="com.chen.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面,ref 要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.chen.services.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
舍弃了第一方式log类和afterlog类
aop编程思想应用在任务完成以后添加代码中 可以减少在迭代的时候修改原有代码,而是进行切面横切进入修改代码 这样就不会出现修改一处 bug出现全盘bug到最后的全盘崩溃