AOP实例 – 环绕增强 日志记录
需求:记录图书的service层的日志操作,到数据库
1. 创建商品日志记录表 Book_Log
2. 完成日志记录表的插入逻辑
3. 环绕增强搜集日志记录参数,并测试
4. 环绕增强调用日志记录表的插入逻辑,完成日志记录功能
5. 正常执行目标类的核心代码
注意:需要采用Spring IOC 模式,加载bean
创建实体类:BookLog
书写相应的BookLogDao,BookLogDaoMapper.xml,BookLogService,BookLogServiceImpl,Junit
public interface BookLogDao {
//新增,入参传实体类名
public int insertLog(BookLog bookLog);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--com.nz.dao.BookDao类路径名-->
<mapper namespace="com.nz.dao.BookLogDao">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.nz.entity.BookLog">
<id column="id" property="id" />
<result column="action_class" property="actionClass" />
<result column="method_name" property="methodName" />
<result column="args" property="args" />
<result column="create_date" property="createDate" />
</resultMap>
<insert id="insertLog">
insert into book_log(action_class,method_name,args)
VALUES(#{actionClass},#{methodName},#{args})
</insert>
</mapper>
public interface BookLogService {
//新增
public boolean addLog(BookLog bookLog);
}
package com.nz.service.impl;
import com.nz.dao.BookLogDao;
import com.nz.entity.Book;
import com.nz.entity.BookLog;
import com.nz.service.BookLogService;
import com.nz.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
/**
* Created by 86185 on 2023/1/31.
*/
public class BookLogServiceImpl implements BookLogService{
@Override
public boolean addLog(BookLog bookLog) {
//获得sqlSession
SqlSession sqlSession = MyBatisUtil.getSqlSession();
//获取dao对象 session 代理
//BookDao.class就是BookDao的类名
BookLogDao bookLogDao = sqlSession.getMapper(BookLogDao.class);
int i = bookLogDao.insertLog(bookLog);
if(i>0){
sqlSession.commit();
return true;
}
// 关闭session
MyBatisUtil.close();
//反参
return false;
}
}
@Test
public void springMethod5() {
BookLogService bookLogService = (BookLogService) context.getBean("bookLogService");
BookLog bookLog = new BookLog();
bookLog.setActionClass("class");bookLog.setMethodName("name");bookLog.setArgs("args");
boolean b1 = bookLogService.addLog(bookLog);
System.out.print(b1);
}
3.
package com.nz.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* Created by 86185 on 2023/1/31.
*/
public class LogInterceptor implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.print("this is my log interceptor");
//获取目标方法名
String methodName = arg0.getMethod().getName();
//获取目标类名
String actionClass = arg0.getThis().getClass().toString();
//获取参数列表
Object[] arguments = arg0.getArguments();
//初始化反参
String args = "";
for(int i=0;i<arguments.length;i++){
args = args + arguments[i]+"-";
}
//添加日志
//放行目标代码
Object object = arg0.proceed();
return object;
}
}
4.
public class BookServiceImpl implements BookService {
@Override
//注解是放到哪里,哪里生效,注解有切入的功能,绑定了增强功能
@AnnotationLog
public boolean addBook(String bookName,String bookAuth,String bookBrief) {
//获得sqlSession
SqlSession sqlSession = MyBatisUtil.getSqlSession();
//获取dao对象 session 代理
//BookDao.class就是BookDao的类名
BookDao bookDao = sqlSession.getMapper(BookDao.class);
int i = bookDao.insertBook(bookName, bookAuth, bookBrief);
sqlSession.commit();
return true;
}
@Test
public void springMethod3(){
BookService bookService = (BookService) context.getBean("bookService");
boolean b1 = bookService.addBook("bookName", "auth", "brief");
System.out.print(b1);
}