SpringAOP中怎么定義切點(diǎn)

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

10年積累的成都網(wǎng)站建設(shè)、網(wǎng)站制作經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有巴中免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

一 配置

<?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()切入點(diǎn)  @AfterReturning(returning="rvt"    , pointcut="SystemArchitecture.myPointcut()")  // 聲明rvt時指定的類型會限制目標(biāo)方法必須返回指定類型的值或沒有返回值  // 此處將rvt的類型聲明為Object,意味著對目標(biāo)方法的返回值不加限制  public void log(Object rvt)  {    System.out.println("獲取目標(biāo)方法返回值:" + rvt);    System.out.println("模擬記錄日志功能...");  }}

三 接口

Hello

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

World

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

四 實(shí)現(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īng)用中的業(yè)務(wù)邏輯方法  public void foo()  {    System.out.println("執(zhí)行Hello組件的foo()方法");  }  // 定義一個addUser()方法,模擬應(yīng)用中的添加用戶的方法  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īng)用中的業(yè)務(wù)邏輯方法  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()方法獲取目標(biāo)方法返回值:null模擬記錄日志功能...執(zhí)行Hello組件的addUser添加用戶:孫悟空獲取目標(biāo)方法返回值:20模擬記錄日志功能...執(zhí)行World組件的bar()方法獲取目標(biāo)方法返回值:null模擬記錄日志功能...

看完上述內(nèi)容,你們對SpringAOP中怎么定義切點(diǎn)有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

分享名稱:SpringAOP中怎么定義切點(diǎn)
URL網(wǎng)址:http://bm7419.com/article2/jccjoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)搜索引擎優(yōu)化、Google、電子商務(wù)、網(wǎng)站設(shè)計(jì)公司、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)