今天就跟大家聊聊有關如何在Spring AOP中定義切點,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務領域包括:網(wǎng)站設計、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的雞冠網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!
一 配置
<?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.*; @Aspect public 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.*; @Aspect public 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 { // 定義一個簡單方法,模擬應用中的業(yè)務邏輯方法 void foo(); // 定義一個addUser()方法,模擬應用中的添加用戶的方法 int addUser(String name, String pass); }
World
package org.crazyit.app.service; public interface World { // 定義一個簡單方法,模擬應用中的業(yè)務邏輯方法 public void bar(); }
四 實現(xiàn)類
HelloImpl
package org.crazyit.app.service.impl; import org.springframework.stereotype.Component; import org.crazyit.app.service.*; @Component("hello") public class HelloImpl implements Hello { // 定義一個簡單方法,模擬應用中的業(yè)務邏輯方法 public void foo() { System.out.println("執(zhí)行Hello組件的foo()方法"); } // 定義一個addUser()方法,模擬應用中的添加用戶的方法 public int addUser(String name , String pass) { System.out.println("執(zhí)行Hello組件的addUser添加用戶:" + name); if(name.length() < 3 || name.length() > 10) { throw new IllegalArgumentException("name參數(shù)的長度必須大于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 { // 定義一個簡單方法,模擬應用中的業(yè)務邏輯方法 public void bar() { System.out.println("執(zhí)行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) { // 創(chuàng)建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(); } }
六 測試結(jié)果
執(zhí)行Hello組件的foo()方法
獲取目標方法返回值:null
模擬記錄日志功能...
執(zhí)行Hello組件的addUser添加用戶:孫悟空
獲取目標方法返回值:20
模擬記錄日志功能...
執(zhí)行World組件的bar()方法
獲取目標方法返回值:null
模擬記錄日志功能...
看完上述內(nèi)容,你們對如何在Spring AOP中定義切點有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
新聞名稱:如何在SpringAOP中定義切點
本文地址:http://www.chinadenli.net/article18/iepodp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設計公司、App設計、響應式網(wǎng)站、微信公眾號、網(wǎng)站設計、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)