這篇文章將為大家詳細(xì)講解有關(guān)spring AOP中After如何增強(qiáng),小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)服務(wù)項目包括勃利網(wǎng)站建設(shè)、勃利網(wǎng)站制作、勃利網(wǎng)頁制作以及勃利網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,勃利網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到勃利省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
一 配置
<?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>
二 切面類
package org.crazyit.app.aspect; import org.aspectj.lang.annotation.*; import org.aspectj.lang.*; // 定義一個切面 @Aspect public class ReleaseAspect { // 匹配org.crazyit.app.service包下所有類的、 // 所有方法的執(zhí)行作為切入點 @After("execution(* org.crazyit.app.service.*.*(..))") public void release() { System.out.println("模擬方法結(jié)束后的釋放資源..."); } }
三 接口
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(); }
四 實現(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()方法
模擬方法結(jié)束后的釋放資源...
執(zhí)行Hello組件的addUser添加用戶:悟空
模擬方法結(jié)束后的釋放資源...
Exception in thread "main" java.lang.IllegalArgumentException: name參數(shù)的長度必須大于3,小于10!
at org.crazyit.app.service.impl.HelloImpl.addUser(HelloImpl.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:43)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy7.addUser(Unknown Source)
at lee.BeanTest.main(BeanTest.java:26)
關(guān)于“spring AOP中After如何增強(qiáng)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
文章名稱:springAOP中After如何增強(qiáng)
本文鏈接:http://chinadenli.net/article46/gogseg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、面包屑導(dǎo)航、做網(wǎng)站、App開發(fā)、品牌網(wǎng)站建設(shè)、網(wǎng)站營銷
聲明:本網(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)