Spring怎么實(shí)現(xiàn)AOP添加日志記錄功能

本篇內(nèi)容介紹了“Spring怎么實(shí)現(xiàn)AOP添加日志記錄功能”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

農(nóng)安ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!

需求,在調(diào)用業(yè)務(wù)方法的時(shí)候,在被調(diào)用的業(yè)務(wù)方法的前面和后面添加上日志記錄功能

整體架構(gòu):

日志處理類:

package aop;import java.util.Arrays;import org.apache.log4j.Logger;import org.aspectj.lang.JoinPoint;//日志處理類 增強(qiáng)處理類-日志public class UserServiceLogger {  private Logger logger = Logger.getLogger(UserServiceLogger.class);  // 前置增強(qiáng)  public void before(JoinPoint joinPoint) {    logger.info("調(diào)用" + joinPoint.getTarget() + "的"        + joinPoint.getSignature() + "方法,方法參數(shù)是:"        + Arrays.toString(joinPoint.getArgs()));  }  // 后置增強(qiáng)  public void afterReturning(JoinPoint joinPoint,Object result) {    logger.info("調(diào)用" + joinPoint.getTarget() + "的"        + joinPoint.getSignature() + "方法,方法的返回值是:"        +result);  }}

下面是一個(gè)三層架構(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類,實(shí)現(xiàn)IDao接口,負(fù)責(zé)User類的持久化操作 */public class UserDaoImpl implements UserDao {  public void save(User user) {    // 這里并未實(shí)現(xiàn)完整的數(shù)據(jù)庫操作,僅為說明問題    System.out.println("保存用戶信息到數(shù)據(jù)庫");  }}

package entity;/** * 用戶實(shí)體類 */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ù)類,實(shí)現(xiàn)對(duì)User功能的業(yè)務(wù)管理 */public class UserServiceImpl implements UserService {  // 聲明接口類型的引用,和具體實(shí)現(xiàn)類解耦合  private UserDao dao;  // dao 屬性的setter訪問器,會(huì)被Spring調(diào)用,實(shí)現(xiàn)設(shè)值注入  public void setDao(UserDao dao) {    this.dao = dao;  }  public void addNewUser(User user) {    // 調(diào)用用戶DAO的方法保存用戶信息    dao.save(user);  }}

編寫單元測(cè)試方法: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>  <!-- 聲明增強(qiáng)方法所在的Bean -->  <bean id="theLogger" class="aop.UserServiceLogger"></bean>  <!-- 配置切面 -->  <aop:config>    <!-- 定義切入點(diǎn) -->    <aop:pointcut id="pointcut"      expression="execution(public void addNewUser(entity.User))" />    <!-- 引用包含增強(qiáng)方法的Bean -->    <aop:aspect ref="theLogger">      <!-- 將before()方法定義為前置增強(qiáng)并引用pointcut切入點(diǎn) -->      <aop:before method="before" pointcut-ref="pointcut"></aop:before>      <!-- 將afterReturning()方法定義為后置增強(qiáng)并引用pointcut切入點(diǎn) -->      <!-- 通過returning屬性指定為名為result的參數(shù)注入返回值 -->      <aop:after-returning method="afterReturning"       pointcut-ref="pointcut" returning="result" />    </aop:aspect>  </aop:config></beans>

運(yùn)行結(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 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 -調(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

“Spring怎么實(shí)現(xiàn)AOP添加日志記錄功能”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

分享名稱:Spring怎么實(shí)現(xiàn)AOP添加日志記錄功能
文章轉(zhuǎn)載:http://www.muchs.cn/article30/pgdgpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司定制開發(fā)、營銷型網(wǎng)站建設(shè)、外貿(mào)建站、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司