Spring框架实现AOP添加日志记录功能过程详解

网友投稿 400 2023-07-02

Spring框架实现AOP添加日志记录功能过程详解

Spring框架实现AOP添加日志记录功能过程详解

这篇文章主要介绍了Spring框架实现AOP添加日志记录功能过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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

整体架构:

日志处理类:

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 & settehttp://r

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核心配置文件:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.2.xsd">

expression="execution(public void addNewUser(entity.User))" />

pointcut-ref="pointcut" returning="result" />

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.2.xsd">

expression="execution(public void addNewUser(entity.User))" />

pointcut-ref="pointcut" returning="result" />

expression="execution(public void addNewUser(entity.User))" />

pointcut-ref="pointcut" returning="result" />

pointcut-ref="pointcut" returning="result" />

运行结果:

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 hierarchy

12-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 hierarchy

12-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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring框架构造注入type属性实例详解
下一篇:SpringBoot启动后启动内嵌浏览器的方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~