Spring怎么实现AOP添加日志记录功能

本篇内容介绍了“Spring怎么实现AOP添加日志记录功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

农安ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!

需求,在调用业务方法的时候,在被调用的业务方法的前面和后面添加上日志记录功能

整体架构:

日志处理类:

package aop;import java.util.Arrays;import org.apache.log4j.Logger;import org.aspectj.lang.JoinPoint;//日志处理类 增强处理类-日志public class UserServiceLogger {  private Logger logger = Logger.getLogger(UserServiceLogger.class);  // 前置增强  public void before(JoinPoint joinPoint) {    logger.info("调用" + joinPoint.getTarget() + "的"        + joinPoint.getSignature() + "方法,方法参数是:"        + Arrays.toString(joinPoint.getArgs()));  }  // 后置增强  public void afterReturning(JoinPoint joinPoint,Object result) {    logger.info("调用" + joinPoint.getTarget() + "的"        + joinPoint.getSignature() + "方法,方法的返回值是:"        +result);  }}

下面是一个三层架构模式: package dao;import entity.User;/** * 增加DAO接口,定义了所需的持久化方法 */public interface UserDao {  public void save(User user);}

package dao.impl;import dao.UserDao;import entity.User;/** * 用户DAO类,实现IDao接口,负责User类的持久化操作 */public class UserDaoImpl implements UserDao {  public void save(User user) {    // 这里并未实现完整的数据库操作,仅为说明问题    System.out.println("保存用户信息到数据库");  }}

package entity;/** * 用户实体类 */public class User implements java.io.Serializable {  private Integer id; // 用户ID  private String username; // 用户名  private String password; // 密码  private String email; // 电子邮件  // getter & setter  public Integer getId() {    return id;  }  public void setId(Integer id) {    this.id = id;  }  public String getUsername() {    return username;  }  public void setUsername(String username) {    this.username = username;  }  public String getPassword() {    return password;  }  public void setPassword(String password) {    this.password = password;  }  public String getEmail() {    return email;  }  public void setEmail(String email) {    this.email = email;  }}

package service;import entity.User;/** * 用户业务接口,定义了所需的业务方法 */public interface UserService {  public void addNewUser(User user);}

package service.impl;import service.UserService;import dao.UserDao;import entity.User;/** * 用户业务类,实现对User功能的业务管理 */public class UserServiceImpl implements UserService {  // 声明接口类型的引用,和具体实现类解耦合  private UserDao dao;  // dao 属性的setter访问器,会被Spring调用,实现设值注入  public void setDao(UserDao dao) {    this.dao = dao;  }  public void addNewUser(User user) {    // 调用用户DAO的方法保存用户信息    dao.save(user);  }}

编写单元测试方法:package test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import service.UserService;import service.impl.UserServiceImpl;import entity.User;public class AopTest {  @Test  public void aopTest() {    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");    UserService service = (UserService) ctx.getBean("service");        User user = new User();    user.setId(1);    user.setUsername("test");    user.setPassword("123456");    user.setEmail("test@xxx.com");    service.addNewUser(user);  }}

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  http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">  <bean id="dao" class="dao.impl.UserDaoImpl"></bean>  <bean id="service" class="service.impl.UserServiceImpl">    <property name="dao" ref="dao"></property>  </bean>  <!-- 声明增强方法所在的Bean -->  <bean id="theLogger" class="aop.UserServiceLogger"></bean>  <!-- 配置切面 -->  <aop:config>    <!-- 定义切入点 -->    <aop:pointcut id="pointcut"      expression="execution(public void addNewUser(entity.User))" />    <!-- 引用包含增强方法的Bean -->    <aop:aspect ref="theLogger">      <!-- 将before()方法定义为前置增强并引用pointcut切入点 -->      <aop:before method="before" pointcut-ref="pointcut"></aop:before>      <!-- 将afterReturning()方法定义为后置增强并引用pointcut切入点 -->      <!-- 通过returning属性指定为名为result的参数注入返回值 -->      <aop:after-returning method="afterReturning"       pointcut-ref="pointcut" returning="result" />    </aop:aspect>  </aop:config></beans>

运行结果:

12-29 15:13:06[INFO]org.springframework.context.support.ClassPathXmlApplicationContext -Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2db0f6b2: startup date [Sun Dec 29 15:13:06 CST 2019]; root of context hierarchy12-29 15:13:06[INFO]org.springframework.beans.factory.xml.XmlBeanDefinitionReader -Loading XML bean definitions from class path resource [applicationContext.xml]12-29 15:13:06[INFO]org.springframework.beans.factory.support.DefaultListableBeanFactory -Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3701eaf6: defining beans [userDao,service,theLogger,org.springframework.aop.config.internalAutoProxyCreator,pointcut,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1]; root of factory hierarchy12-29 15:13:06[INFO]aop.UserServiceLogger -调用service.impl.UserServiceImpl@3c130745的void service.UserService.addNewUser(User)方法,方法参数是:[entity.User@2e4b8173]保存用户信息到数据库12-29 15:13:06[INFO]aop.UserServiceLogger -调用service.impl.UserServiceImpl@3c130745的void service.UserService.addNewUser(User)方法,方法的返回值是:null

“Spring怎么实现AOP添加日志记录功能”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!

分享名称:Spring怎么实现AOP添加日志记录功能
文章转载:https://www.cdcxhl.com/article30/pgdgpo.html

成都网站建设公司_创新互联,为您提供网站设计公司定制开发营销型网站建设外贸建站外贸网站建设网站设计

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站建设网站维护公司