最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring框架實(shí)現(xiàn)AOP添加日志記錄功能過程詳解

 更新時間:2019年12月31日 11:11:23   作者:dongyaotou  
這篇文章主要介紹了Spring框架實(shí)現(xiàn)AOP添加日志記錄功能過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了Spring框架實(shí)現(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;

//日志處理類 增強(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ò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)對User功能的業(yè)務(wù)管理
 */
public class UserServiceImpl implements UserService {

  // 聲明接口類型的引用,和具體實(shí)現(xiàn)類解耦合
  private UserDao dao;

  // dao 屬性的setter訪問器,會被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);
  }
}
編寫單元測試方法:
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 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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中EnumMap代替序數(shù)索引代碼詳解

    Java中EnumMap代替序數(shù)索引代碼詳解

    這篇文章主要介紹了Java中EnumMap代替序數(shù)索引代碼詳解,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • java并發(fā)無鎖多線程單線程示例詳解

    java并發(fā)無鎖多線程單線程示例詳解

    這篇文章主要為大家介紹了java并發(fā)無鎖多線程單線程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Springboot集成JAXB返回xml格式

    Springboot集成JAXB返回xml格式

    這篇文章主要為大家詳細(xì)介紹了Springboot如何集成JAXB返回xml格式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-12-12
  • 圖解Java經(jīng)典算法冒泡排序的原理與實(shí)現(xiàn)

    圖解Java經(jīng)典算法冒泡排序的原理與實(shí)現(xiàn)

    冒泡排序是一種簡單的排序算法,它也是一種穩(wěn)定排序算法。其實(shí)現(xiàn)原理是重復(fù)掃描待排序序列,并比較每一對相鄰的元素,當(dāng)該對元素順序不正確時進(jìn)行交換。一直重復(fù)這個過程,直到?jīng)]有任何兩個相鄰元素可以交換,就表明完成了排序
    2022-09-09
  • JetBrains?產(chǎn)品輸入激活碼?Key?is?invalid?完美解決方案

    JetBrains?產(chǎn)品輸入激活碼?Key?is?invalid?完美解決方案

    JetBrains?系列產(chǎn)品(IDEA、Pycharm?等)使用本站破解教程?(opens?new?window),在輸入激活碼時,部分小伙伴反應(yīng)說提示?Key?is?invalid?無法激活,今天小編給大家分享完美解決方案,感興趣的朋友跟隨小編一起看看吧
    2022-11-11
  • java開發(fā)RocketMQ消息中間件原理基礎(chǔ)詳解

    java開發(fā)RocketMQ消息中間件原理基礎(chǔ)詳解

    最近 RocketMQ 剛剛上生產(chǎn)環(huán)境,閑暇之時在這里做一些分享,主要目的是讓初學(xué)者能快速上手RocketMQ,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • Spring Security跳轉(zhuǎn)頁面失敗問題解決

    Spring Security跳轉(zhuǎn)頁面失敗問題解決

    這篇文章主要介紹了Spring Security跳轉(zhuǎn)頁面失敗問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01
  • Java中的Lombok使用詳解

    Java中的Lombok使用詳解

    這篇文章主要介紹了Java中的Lombok使用詳解,Lombok是一個在Java開發(fā)過程中使用注解的方式,用于簡化JavaBean的編寫,避免冗余和樣板式代碼的插入,使類的編寫更加簡潔,需要的朋友可以參考下
    2023-08-08
  • Java之Default關(guān)鍵字的兩種使用方式

    Java之Default關(guān)鍵字的兩種使用方式

    Java關(guān)鍵字default主要有兩種使用場景:一是在switch語句中作為默認(rèn)執(zhí)行的分支;二是在接口中定義默認(rèn)方法,這是Java 8新增的特性,允許接口包含具體實(shí)現(xiàn)的方法,在switch中,當(dāng)沒有匹配的case時,執(zhí)行default分支
    2024-09-09
  • 詳解JUC并發(fā)編程中的進(jìn)程與線程學(xué)習(xí)

    詳解JUC并發(fā)編程中的進(jìn)程與線程學(xué)習(xí)

    這篇文章主要為大家詳細(xì)介紹了JUC并發(fā)編程中的進(jìn)程與線程學(xué)習(xí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03

最新評論

武强县| 建昌县| 明光市| 石屏县| 高青县| 麻江县| 体育| 兴安盟| 富阳市| 淮滨县| 微山县| 鹿泉市| 毕节市| 绥芬河市| 广宁县| 宣城市| 盐城市| 南木林县| 轮台县| 英山县| 鱼台县| 英山县| 竹北市| 洪泽县| 新安县| 阿坝| 密山市| 临海市| 汶上县| 东安县| 卢氏县| 石台县| 九江市| 兴文县| 日照市| 临安市| 崇明县| 新乐市| 丰都县| 即墨市| 嘉荫县|