Spring框架實現(xiàn)AOP添加日志記錄功能過程詳解
這篇文章主要介紹了Spring框架實現(xiàn)AOP添加日志記錄功能過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
需求,在調(diào)用業(yè)務(wù)方法的時候,在被調(diào)用的業(yè)務(wù)方法的前面和后面添加上日志記錄功能
整體架構(gòu):
日志處理類:
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("調(diào)用" + joinPoint.getTarget() + "的" + joinPoint.getSignature() + "方法,方法參數(shù)是:" + Arrays.toString(joinPoint.getArgs())); } // 后置增強 public void afterReturning(JoinPoint joinPoint,Object result) { logger.info("調(diào)用" + joinPoint.getTarget() + "的" + joinPoint.getSignature() + "方法,方法的返回值是:" +result); } }
下面是一個三層架構(gòu)模式: package dao; import entity.User; /** * 增加DAO接口,定義了所需的持久化方法 */ public interface UserDao { public void save(User user); }
package dao.impl; import dao.UserDao; import entity.User; /** * 用戶DAO類,實現(xiàn)IDao接口,負(fù)責(zé)User類的持久化操作 */ public class UserDaoImpl implements UserDao { public void save(User user) { // 這里并未實現(xiàn)完整的數(shù)據(jù)庫操作,僅為說明問題 System.out.println("保存用戶信息到數(shù)據(jù)庫"); } }
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; /** * 用戶業(yè)務(wù)接口,定義了所需的業(yè)務(wù)方法 */ public interface UserService { public void addNewUser(User user); }
package service.impl; import service.UserService; import dao.UserDao; import entity.User; /** * 用戶業(yè)務(wù)類,實現(xiàn)對User功能的業(yè)務(wù)管理 */ public class UserServiceImpl implements UserService { // 聲明接口類型的引用,和具體實現(xiàn)類解耦合 private UserDao dao; // dao 屬性的setter訪問器,會被Spring調(diào)用,實現(xiàn)設(shè)值注入 public void setDao(UserDao dao) { this.dao = dao; } public void addNewUser(User user) { // 調(diào)用用戶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的參數(shù)注入返回值 --> <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result" /> </aop:aspect> </aop:config> </beans>
運行結(jié)果:
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 -調(diào)用service.impl.UserServiceImpl@3c130745的void service.UserService.addNewUser(User)方法,方法參數(shù)是:[entity.User@2e4b8173] 保存用戶信息到數(shù)據(jù)庫 12-29 15:13:06[INFO]aop.UserServiceLogger -調(diào)用service.impl.UserServiceImpl@3c130745的void service.UserService.addNewUser(User)方法,方法的返回值是:null
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Java Swing JFrame窗口的實現(xiàn)
欄 目:Java
下一篇:SpringBoot項目執(zhí)行腳本 自動拉取最新代碼并重啟的實例內(nèi)容
本文標(biāo)題:Spring框架實現(xiàn)AOP添加日志記錄功能過程詳解
本文地址:http://mengdiqiu.com.cn/a1/Java/8803.html
您可能感興趣的文章
- 01-10Java實現(xiàn)動態(tài)模擬時鐘
- 01-10Springboot中@Value的使用詳解
- 01-10利用Java實現(xiàn)復(fù)制Excel工作表功能
- 01-10JavaWeb實現(xiàn)郵件發(fā)送功能
- 01-10Java實現(xiàn)動態(tài)數(shù)字時鐘
- 01-10java實現(xiàn)液晶數(shù)字字體顯示當(dāng)前時間
- 01-10springboot實現(xiàn)文件上傳步驟解析
- 01-10java實現(xiàn)的順時針/逆時針打印矩陣操作示例
- 01-10springboot jta atomikos實現(xiàn)分布式事物管理
- 01-10Java后臺防止客戶端重復(fù)請求、提交表單實現(xiàn)原理


閱讀排行
本欄相關(guān)
- 01-10Java實現(xiàn)動態(tài)模擬時鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實現(xiàn)郵件發(fā)送功能
- 01-10利用Java實現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實現(xiàn)動態(tài)數(shù)字時鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實例
- 01-10java實現(xiàn)液晶數(shù)字字體顯示當(dāng)前時間
- 01-10基于Java驗證jwt token代碼實例
- 01-10Java動態(tài)顯示當(dāng)前日期和時間
- 01-10淺談Java中真的只有值傳遞么
隨機閱讀
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改