SpringAop日誌管理

Spring AOP 

 

1.對AOP的理解 ——  分工來做各個部分,運行時候整合的思想


2.理解 面向過程,面向對象,面向切面 的思想

1)面向過程:房間裝修時,準備裝一個燈,就拉一根電線,連接燈。


2)面向對象:設計房間中哪些位置需要使用電線接口,然後在相應的位置設置電線接口,以備以後使用。


3)面向切面:裝修房子,先設計需要在哪些地方裝上電線接口,就將電線接口先設置好並且不打開接口,此處即爲連接點,當此處電線切口確實需要使用時將接口打開插電器即爲切入點。


方面:功能(登陸 日誌)

目標:主要方面(登陸)

切面:切入點 植入 通知的綜合體

連接點:可以插入副功能(日誌)的地方

切入點:準備插入副功能的地方

通知:對副功能的封裝對象

植入:將通知插入切入點


3.實現登陸和日誌管理(使用Spring AOP)


1)LoginService   LogService   TestMain

2)用Spring 管理  LoginService 和 LogService 的對象

3)確定哪些連接點是切入點,在配置文件中

4)將LogService封裝爲通知

5)將通知植入到切入點

6)客戶端調用目標



  1. <aop:config>
  2.     <aop:pointcut expression="execution(* cn.com.spring.service.impl.*.*(..))" id="myPointcut"/>
  3.     <!--將哪個-->
  4.     <aop:aspect id="dd" ref="logService">
  5.       <aop:before method="log" pointcut-ref="myPointcut"/>
  6.     </aop:aspect>
  7. </aop:config>

execution(* * cn.com.spring.service.impl.*.*(..))

1)* 所有的修飾符

2)* 所有的返回類型

3)* 所有的類名

4)* 所有的方法名

5)* ..所有的參數名


1.ILoginService.java


  1. package cn.com.spring.service;

  2. public interface ILoginService {
  3.     public boolean login(String userName, String password);
  4. }
2.LoginServiceImpl.java


  1. package cn.com.spring.service.impl;

  2. import cn.com.spring.service.ILoginService;

  3. public class LoginServiceImpl implements ILoginService {

  4.     public boolean login(String userName, String password) {
  5.         System.out.println("login:" + userName + "," + password);
  6.         return true;
  7.     }

  8. }

3.ILogService.java


  1. package cn.com.spring.service;

  2. import org.aspectj.lang.JoinPoint;

  3. public interface ILogService {
  4.     //無參的日誌方法
  5.     public void log();
  6.     //有參的日誌方法
  7.     public void logArg(JoinPoint point);
  8.     //有參有返回值的方法
  9.     public void logArgAndReturn(JoinPoint point,Object returnObj);
  10. }

4.LogServiceImpl.java


  1. package cn.com.spring.service.impl;

  2. import org.aspectj.lang.JoinPoint;

  3. import cn.com.spring.service.ILogService;

  4. public class LogServiceImpl implements ILogService {

  5.     @Override
  6.     public void log() {
  7.         System.out.println("*************Log*******************");
  8.     }
  9.     
  10.     //有參無返回值的方法
  11.     public void logArg(JoinPoint point) {
  12.         //此方法返回的是一個數組,數組中包括request以及ActionCofig等類對象
  13.         Object[] args = point.getArgs();
  14.         System.out.println("目標參數列表:");
  15.         if (args != null) {
  16.             for (Object obj : args) {
  17.                 System.out.println(obj + ",");
  18.             }
  19.             System.out.println();
  20.         }
  21.     }

  22.     //有參並有返回值的方法
  23.     public void logArgAndReturn(JoinPoint point, Object returnObj) {
  24.         //此方法返回的是一個數組,數組中包括request以及ActionCofig等類對象
  25.         Object[] args = point.getArgs();
  26.         System.out.println("目標參數列表:");
  27.         if (args != null) {
  28.             for (Object obj : args) {
  29.                 System.out.println(obj + ",");
  30.             }
  31.             System.out.println();
  32.             System.out.println("執行結果是:" + returnObj);
  33.         }
  34.     }
  35. }
5.applicationContext.java


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:p="http://www.springframework.org/schema/p"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans 
  6.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7.     http://www.springframework.org/schema/aop 
  8.     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

  9.     <bean id="logService" class="cn.com.spring.service.impl.LogServiceImpl"></bean>
  10.     <bean id="loginService" class="cn.com.spring.service.impl.LoginServiceImpl"></bean>

  11.     <aop:config>
  12.         <!-- 切入點 -->
  13.         <aop:pointcut
  14.             expression="execution(* cn.com.spring.service.impl.LoginServiceImpl.*(..))"
  15.             id="myPointcut" />
  16.         <!-- 切面: 將哪個對象中的哪個方法,織入到哪個切入點 -->
  17.         <aop:aspect id="dd" ref="logService">
  18.             <!-- 前置通知
  19.             <aop:before method="log" pointcut-ref="myPointcut" />
  20.             <aop:after method="logArg" pointcut-ref="myPointcut"> 
  21.     -->
  22.             <aop:after-returning method="logArgAndReturn" returning="returnObj" pointcut-ref="myPointcut"/>
  23.         </aop:aspect>
  24.     </aop:config>
  25. </beans>
6.TestMain.java


  1. public class TestMain {
  2. public static void testSpringAOP(){
  3.         ApplicationContext ctx = new ClassPathXmlApplicationContext("app*.xml");
  4.         
  5.         ILoginService loginService = (ILoginService)ctx.getBean("loginService");
  6.         loginService.login("zhangsan", "12344");
  7. }
  8. public static void main(String[] args) {
  9. testSpringAOP();
  10. }
  11. }
7.輸出結果:

  1. login:zhangsan,12344
  2. 目標參數列表:
  3. zhangsan,
  4. 12344,

  5. 執行結果是:true
解析:1.先調用了login()方法System.out.println("login:" + userName + "," + password);

     2.再調用了logArgAndReturn()方法輸出了日誌,並且返回了login()方法是否成功


  1. System.out.println("目標參數列表:");
  2.         if (args != null) {
  3.             for (Object obj : args) {
  4.                 System.out.println(obj + ",");
  5.             }
  6.             System.out.println();
  7.             System.out.println("執行結果是:" + returnObj);
  8.         }

發佈了46 篇原創文章 · 獲贊 2 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章