您好,登錄后才能下訂單哦!
小編給大家分享一下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 & 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 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
以上是“Spring框架如何實現AOP添加日志記錄功能”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。