欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

需求,在調(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);
  }
}
編寫單元測試方法:
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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 利用ThreadLocal實(shí)現(xiàn)一個(gè)上下文管理組件

    利用ThreadLocal實(shí)現(xiàn)一個(gè)上下文管理組件

    本文基于ThreadLocal原理,實(shí)現(xiàn)了一個(gè)上下文狀態(tài)管理組件Scope,通過開啟一個(gè)自定義的Scope,在Scope范圍內(nèi),可以通過Scope各個(gè)方法讀寫數(shù)據(jù),感興趣的可以了解一下
    2022-10-10
  • 使用java實(shí)現(xiàn)銀行家算法

    使用java實(shí)現(xiàn)銀行家算法

    這篇文章主要為大家詳細(xì)介紹了如何使用java實(shí)現(xiàn)銀行家算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • Java線程池的四種拒絕策略詳解

    Java線程池的四種拒絕策略詳解

    jdk1.5 版本新增了JUC并發(fā)編程包,極大的簡化了傳統(tǒng)的多線程開發(fā),下面這篇文章主要介紹了Java線程池的四種拒絕策略的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • Spring Boot 中PageHelper 插件使用配置思路詳解

    Spring Boot 中PageHelper 插件使用配置思路詳解

    這篇文章主要介紹了Spring Boot 中PageHelper 插件使用配置及實(shí)現(xiàn)思路,通過引入myabtis和pagehelper依賴,在yml中配置mybatis掃描和實(shí)體類,具體實(shí)現(xiàn)方法跟隨小編一起看看吧
    2021-08-08
  • Java語言實(shí)現(xiàn)Blowfish加密算法完整代碼分享

    Java語言實(shí)現(xiàn)Blowfish加密算法完整代碼分享

    這篇文章主要介紹了Java語言實(shí)現(xiàn)Blowfish加密算法完整代碼分享,簡單介紹了blowfish加密算法,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-11-11
  • Struts2實(shí)現(xiàn)文件上傳功能實(shí)例解析

    Struts2實(shí)現(xiàn)文件上傳功能實(shí)例解析

    這篇文章主要介紹了Struts2實(shí)現(xiàn)文件上傳功能實(shí)例解析,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-01-01
  • IDEA創(chuàng)建Maven工程Servlet的詳細(xì)教程

    IDEA創(chuàng)建Maven工程Servlet的詳細(xì)教程

    這篇文章主要介紹了IDEA創(chuàng)建Maven工程Servlet的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 詳解Spring中使用xml配置bean的細(xì)節(jié)

    詳解Spring中使用xml配置bean的細(xì)節(jié)

    本篇文章主要介紹了Spring中使用xml配置bean的細(xì)節(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • 深入分析@Resource和@Autowired注解區(qū)別

    深入分析@Resource和@Autowired注解區(qū)別

    這篇文章主要為大家介紹了深入分析@Resource和@Autowired注解區(qū)別,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • Spring框架學(xué)習(xí)之Spring?@Autowired實(shí)現(xiàn)自動(dòng)裝配的代碼

    Spring框架學(xué)習(xí)之Spring?@Autowired實(shí)現(xiàn)自動(dòng)裝配的代碼

    自動(dòng)裝配就是說,你不用手動(dòng)實(shí)現(xiàn)bean之間的組合關(guān)系,只要使用了@Autowired注解,程序就會(huì)自動(dòng)的注入這個(gè)需要的bean,前提是你的Spring容器有這個(gè)bean,這篇文章主要介紹了Spring?@Autowired實(shí)現(xiàn)自動(dòng)裝配,需要的朋友可以參考下
    2021-12-12

最新評(píng)論