溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringAOP中怎么定義切點

發布時間:2021-07-22 16:28:25 來源:億速云 閱讀:181 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關SpringAOP中怎么定義切點,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

一 配置

<?xml version="1.0" encoding="GBK"?><beans xmlns="http://www.springframework.org/schema/beans"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:context="http://www.springframework.org/schema/context"   xmlns:aop="http://www.springframework.org/schema/aop"   xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd   http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-4.0.xsd   http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">   <!-- 指定自動搜索Bean組件、自動搜索切面類 -->   <context:component-scan      base-package="org.crazyit.app.service      ,org.crazyit.app.aspect">      <context:include-filter type="annotation"        expression="org.aspectj.lang.annotation.Aspect" />   </context:component-scan>   <!-- 啟動@AspectJ支持 -->   <aop:aspectj-autoproxy /></beans>

二 切面類

SystemArchitecture

package org.crazyit.app.aspect;import org.aspectj.lang.annotation.*;@Aspectpublic class SystemArchitecture{   @Pointcut("execution(* org.crazyit.app.service.impl.*.*(..))")   public void myPointcut(){}}

LogAspect

package org.crazyit.app.aspect;import org.aspectj.lang.annotation.*;import org.aspectj.lang.*;@Aspectpublic class LogAspect{  // 直接使用SystemArchitecture切面類的myPointcut()切入點  @AfterReturning(returning="rvt"    , pointcut="SystemArchitecture.myPointcut()")  // 聲明rvt時指定的類型會限制目標方法必須返回指定類型的值或沒有返回值  // 此處將rvt的類型聲明為Object,意味著對目標方法的返回值不加限制  public void log(Object rvt)  {    System.out.println("獲取目標方法返回值:" + rvt);    System.out.println("模擬記錄日志功能...");  }}

三 接口

Hello

package org.crazyit.app.service;public interface Hello {   // 定義一個簡單方法,模擬應用中的業務邏輯方法   void foo();   // 定義一個addUser()方法,模擬應用中的添加用戶的方法   int addUser(String name, String pass);}

World

package org.crazyit.app.service;public interface World {   // 定義一個簡單方法,模擬應用中的業務邏輯方法   public void bar();}

四 實現類

HelloImpl

package org.crazyit.app.service.impl;import org.springframework.stereotype.Component;import org.crazyit.app.service.*;@Component("hello")public class HelloImpl implements Hello{  // 定義一個簡單方法,模擬應用中的業務邏輯方法  public void foo()  {    System.out.println("執行Hello組件的foo()方法");  }  // 定義一個addUser()方法,模擬應用中的添加用戶的方法  public int addUser(String name , String pass)  {    System.out.println("執行Hello組件的addUser添加用戶:" + name);    if(name.length() < 3 || name.length() > 10)    {      throw new IllegalArgumentException("name參數的長度必須大于3,小于10!");    }    return 20;  }}

WorldImpl

package org.crazyit.app.service.impl;import org.springframework.stereotype.Component;import org.crazyit.app.service.*;@Component("world")public class WorldImpl implements World {  // 定義一個簡單方法,模擬應用中的業務邏輯方法  public void bar() {    System.out.println("執行World組件的bar()方法");  }}

五 測試類

package lee;import org.springframework.context.*;import org.springframework.context.support.*;import org.crazyit.app.service.*;public class BeanTest{  public static void main(String[] args)  {    // 創建Spring容器    ApplicationContext ctx = new      ClassPathXmlApplicationContext("beans.xml");    Hello hello = ctx.getBean("hello" , Hello.class);    hello.foo();    hello.addUser("孫悟空" , "7788");    World world = ctx.getBean("world" , World.class);    world.bar();  }}

六 測試結果

執行Hello組件的foo()方法獲取目標方法返回值:null模擬記錄日志功能...執行Hello組件的addUser添加用戶:孫悟空獲取目標方法返回值:20模擬記錄日志功能...執行World組件的bar()方法獲取目標方法返回值:null模擬記錄日志功能...

看完上述內容,你們對SpringAOP中怎么定義切點有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女