สารบัญ
1. สปริง AOP ตามการกำหนดค่า XML
2. ใช้คำอธิบายประกอบเพื่อกำหนดค่า AOP
3. ฟังก์ชั่นการตัดจุด
4. หมายเหตุการแจ้งเตือน
5. การกำหนดค่าศูนย์เพื่อใช้สปริง IOC และ AOP
AOP (การเขียนโปรแกรมแบบมุ่งเน้นด้าน) เป็นเทคโนโลยีที่ใช้การควบคุมแบบครบวงจรแบบหลายโมดูลในแนวนอนของฟังก์ชั่นโปรแกรมผ่านวิธีการตรวจสอบล่วงหน้าและตัวแทนแบบไดนามิกในระหว่างการรันไทม์ AOP เป็นอาหารเสริมสำหรับ OOP และเป็นส่วนสำคัญของกรอบฤดูใบไม้ผลิ ส่วนต่าง ๆ ของตรรกะทางธุรกิจสามารถแยกได้โดยใช้ AOP ซึ่งจะช่วยลดการมีเพศสัมพันธ์ระหว่างส่วนต่าง ๆ ของตรรกะทางธุรกิจการปรับปรุงความสามารถในการใช้ซ้ำของโปรแกรมและปรับปรุงประสิทธิภาพของการพัฒนา AOP สามารถแบ่งออกเป็นการทอผ้าแบบคงที่และการทอผ้าแบบไดนามิก การทอผ้าแบบคงที่หมายถึงการเขียนเนื้อหาที่จะทอผ้าลงในโมดูลเป้าหมายก่อนการรวบรวมซึ่งมีค่าใช้จ่ายสูงมาก การทอผ้าแบบไดนามิกไม่จำเป็นต้องเปลี่ยนโมดูลเป้าหมาย Spring Framework ใช้ AOP และการใช้คำอธิบายประกอบเพื่อกำหนดค่า AOP นั้นสะดวกและใช้งานง่ายกว่าการใช้การกำหนดค่า XML
1. สปริง AOP ตามการกำหนดค่า XML
ก่อนที่จะอธิบายคำอธิบายประกอบเพื่อใช้ฟังก์ชั่น AOP ก่อนอื่นให้ใช้การเรียนรู้ก่อนหน้านี้เพื่อกำหนดค่าฟังก์ชั่น Spring AOP โดยใช้ XML เพื่อให้เป็นการเปรียบเทียบและความเข้าใจที่ดีขึ้น
1.1. สร้างโครงการ Maven ใหม่และเพิ่มการอ้างอิง ไฟล์ pom.xml ของโครงการมีดังนี้:
<Project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: schemalocation = "http:/MANCMMMMMMMMMMMMMMMMMMMMMMMMMMBM http://maven.apache.org/xsd/maven-4.0.0.xsd "> <moderversion> 4.0.0 </modelversion> <moderversion> <moderversion> <name> spring052 </name> <url> http://maven.apache.org </url> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <roupId> Junit </groupId> <ratifactId> Junit </artifactid> <scope> ทดสอบ </scope> <scope> <scope> <.10 </เวอร์ชัน> </perentency> <predency> <sderncy> <roupid> org.springframework </groupid> <roupId> org.aspectj </groupId> <ratifactid> AppistjWeaver </artifactid> <cersion> 1.8.9 </เวอร์ชัน> </การพึ่งพาอาศัยกัน> <predency> <roupid> cglib </roupid>
1.2. สร้างคลาสคณิตศาสตร์ที่จะพร็อกซีรหัสมีดังนี้:
แพ็คเกจ com.zhangguo.spring052.aop01;/*** คลาสเป้าหมายที่เป็น proxyed*/คลาสสาธารณะคณิตศาสตร์ {// เพิ่มการเพิ่ม int สาธารณะ (int n1, int n2) {int result = n1+n2; System.out.println (n1+"+"+n2+"="+ผลลัพธ์); ผลการกลับมา; } // ครบกำหนด INT Sub (int N1, int n2) {int result = n1-n2; System.out.println (n1+"-"+n2+"="+ผลลัพธ์); ผลการกลับมา; } // หลาย int mut (int n1, int n2) {int result = n1-n2; System.out.println (n1+"-"+n2+"="+ผลลัพธ์); ผลการกลับมา; } // หลาย int mut (int n1, int n2) {int result = n1*n2; System.out.println (n1+"x"+n2+"="+ผลลัพธ์); ผลการกลับมา; } // ยกเว้น int สาธารณะ div (int n1, int n2) {int result = n1/n2; System.out.println (n1+"/"+n2+"="+ผลลัพธ์); ผลการกลับมา; -1.3. แก้ไขรหัสการแจ้งเตือนรหัส Java ที่ต้องใช้ใน AOP มีดังนี้:
แพ็คเกจ com.zhangguo.spring052.aop01; นำเข้า org.aspectj.lang.joinpoint;/*** คลาสการแจ้งเตือน, ตรรกะข้ามการตัด**/คำแนะนำระดับสาธารณะ {โมฆะสาธารณะก่อน (joinpoint jp) { System.out.println ("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------1.4. กำหนดค่าไฟล์ XML ที่จำเป็นสำหรับการเริ่มต้นคอนเทนเนอร์ เนื้อหาของไฟล์ AOP01.XML มีดังนี้:
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: p = "http://www.springframework.org/schema/p" xmlns: aop = "http://www.springframework.org/schema/aop" xsi: schemalocation = "http:/ http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/aop/aop/aop/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd "> <!-วัตถุพร็อกซี-> <bean id =" Math "> </eparuation> <! <!-ส่วน-> <aop: ref = "advices"> <!-จุด-> <aop: pointcut expression = "การดำเนินการ (* com.zhangguo.spring052.aop01.math.* (.. ))" id = "pointcut1"/> <AOP: After Method = "After" PointCut-ref = "PointCut1"/> </aop: แง่มุม> </aop: config> </ebeans>
1.5. ทดสอบรหัสทดสอบ Java มีดังนี้:
แพ็คเกจ com.zhangguo.spring052.aop01; นำเข้า org.springframework.context.applicationContext; นำเข้า org.springframework.context.support.classpathxmlapplicationcontext; การทดสอบระดับสาธารณะ classpathxmlapplicationContext ("aop01.xml"); คณิตศาสตร์คณิตศาสตร์ = ctx.getBean ("คณิตศาสตร์", math.class); int n1 = 100, n2 = 5; Math.Add (N1, N2); Math.sub (N1, N2); Math.Mut (N1, N2); Math.div (N1, N2); -ผลการทำงาน:
2. ใช้คำอธิบายประกอบเพื่อกำหนดค่า AOP
2.1. ในตัวอย่างก่อนหน้าปรับเปลี่ยนคณิตศาสตร์ระดับพร็อกซี เพื่อที่จะใช้การสแกน IOC @Service จะมีคำอธิบายประกอบในคลาสคณิตศาสตร์และตั้งชื่อถั่วเป็นคณิตศาสตร์ มันเทียบเท่ากับการเพิ่มถั่วลงในไฟล์การกำหนดค่า XML ในตัวอย่างก่อนหน้า <!-วัตถุพร็อกซี-> <bean id = "คณิตศาสตร์"> </ebean> และรหัสของคลาสคณิตศาสตร์มีดังนี้:
แพ็คเกจ com.zhangguo.spring052.aop02; นำเข้า org.springframework.stereotype.service;/*** คลาสเป้าหมายเป็น proxyed*/@บริการ ("คณิตศาสตร์") คณิตศาสตร์สาธารณะ {// เพิ่ม int สาธารณะ System.out.println (n1+"+"+n2+"="+ผลลัพธ์); ผลการกลับมา; } // แทนที่ int sub สาธารณะ (int n1, int n2) {int result = n1-n2; System.out.println (n1+"-"+n2+"="+ผลลัพธ์); ผลการกลับมา; } // หลาย int mut (int n1, int n2) {int result = n1*n2; System.out.println (n1+"x"+n2+"="+ผลลัพธ์); ผลการกลับมา; } // disin public int div (int n1, int n2) {int result = n1/n2; System.out.println (n1+"/"+n2+"="+ผลลัพธ์); ผลการกลับมา; -2.2. แก้ไขคำแนะนำของคลาสการแจ้งเตือนมี 3 คำอธิบายประกอบในรหัส @component หมายความว่าอินสแตนซ์ของคลาสนี้จะได้รับการจัดการโดยคอนเทนเนอร์ Spring IOC; @Aspect หมายความว่ามีการประกาศส่วน; @Before หมายความว่าก่อนหน้านี้คือการแจ้งเตือนล่วงหน้าและมีการประกาศจุดผ่านการดำเนินการพารามิเตอร์ รหัส advices.java มีดังนี้:
แพ็คเกจ com.zhangguo.spring052.aop02; นำเข้า org.aspectj.lang.joinpoint; นำเข้า org.aspectj.lang.annotation.aspect; นำเข้า org.aspectj.lang.annotation.aspect; คลาส, cross-cut logic**/@component@appectpublic คลาส advices {@before ("การดำเนินการ (* com.zhangguo.spring052.aop02.math.* (.. ))") โมฆะสาธารณะก่อน (Joinpoint JP) { System.out.println ("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -รหัสข้างต้นนั้นเหมือนกับการกำหนดค่าต่อไปนี้
<!-การแจ้งเตือน-> <bean id = "advices"> </ebean> <!-การกำหนดค่า AOP-> <aop: config proxy-target-class = "true"> <!-ส่วน-> <aop: ref = "advices"> <! com.zhangguo.spring052.aop01.math.*(.. )) "id =" pointcut1 "/> <!-เชื่อมต่อวิธีการแจ้งเตือนและจุด-> <aop: ก่อนวิธี =" ก่อน "pointcut-ref =" pointcut1 "/>
2.3. เพิ่มไฟล์การกำหนดค่า AOP02.xml และเพิ่ม AOP: Node ApectJ-Autoproxy บนพื้นฐานของการกำหนดค่า IOC Framework Spring จะสร้างพร็อกซีสำหรับถั่วที่กำหนดค่าด้วย APPERSJ โดยอัตโนมัติ แอตทริบิวต์พร็อกซี-เป้าหมาย-class = "true" บ่งชี้ว่าวัตถุเป้าหมายที่เป็นพร็อกซีเป็นคลาสไม่ใช่คลาสที่ใช้อินเทอร์เฟซส่วนใหญ่เพื่อเลือกวิธีพร็อกซีที่แตกต่างกัน
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: p = "http://www.springframework.org/schema/p" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: บริบท = XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://ww.springframework <บริบท: Component-Scan base-package = "com.zhangguo.spring052.aop02"> </บริบท: Component-scan> <aop: Aspectj-autoproxy proxy-target-class = "true">
2.4. ทดสอบการเรียกใช้รหัสการทดสอบ Java มีดังนี้:
แพ็คเกจ com.zhangguo.spring052.aop02; นำเข้า org.springframework.context.applicationContext; นำเข้า org.springframework.context.support.classpathxmlapplicationcontext; การทดสอบคลาสสาธารณะ classpathxmlapplicationContext ("aop02.xml"); คณิตศาสตร์คณิตศาสตร์ = ctx.getBean ("คณิตศาสตร์", math.class); int n1 = 100, n2 = 5; Math.Add (N1, N2); Math.sub (N1, N2); Math.Mut (N1, N2); Math.div (N1, N2); -ผลการทำงาน:
3. ฟังก์ชั่นการตัดจุด
ฟังก์ชั่น Tangent Point สามารถวางตำแหน่งไปยังตำแหน่งตรรกะตามขวางที่แม่นยำ ในตัวอย่างก่อนหน้านี้เราได้ใช้การดำเนินการเท่านั้น (* com.zhangguo.spring052.aop02.math.* (.. )) การดำเนินการเป็นฟังก์ชัน Tangent Point แต่ฟังก์ชั่นอยู่ที่ระดับเดียวเท่านั้น หากช่วงที่เราต้องการสานคือชั้นเรียนหรือคำอธิบายประกอบการดำเนินการจะไม่ใช้งานง่าย ในความเป็นจริงมีฟังก์ชั่นแทนเจนต์ 9 จุดซึ่งมีการกำหนดเป้าหมายที่แตกต่างกัน
@aspectj ใช้นิพจน์การตัดจุดพิเศษของ Appistj เพื่ออธิบายส่วน Experitsj Expressions ที่สนับสนุนโดยฤดูใบไม้ผลิสามารถแบ่งออกเป็นสี่หมวดหมู่:
ฟังก์ชั่นการตัดจุด: กำหนดจุดเชื่อมต่อโดยอธิบายข้อมูลเมธอดคลาสเป้าหมาย
ฟังก์ชั่นพารามิเตอร์วิธีการทำจุด: กำหนดจุดเชื่อมต่อโดยอธิบายข้อมูลพารามิเตอร์ของวิธีการคลาสเป้าหมาย
ฟังก์ชั่นการตัดจุดเป้าหมาย: กำหนดจุดเชื่อมต่อโดยอธิบายข้อมูลประเภทคลาสเป้าหมาย
ฟังก์ชั่นการตัดคลาสตัวแทน: กำหนดจุดเชื่อมต่อโดยอธิบายข้อมูลคลาสพร็อกซี
ฟังก์ชั่นการแสดงออกทางภาพทั่วไป:
ฟังก์ชั่นการตัดแบบจุดที่ใช้กันมากที่สุดคือ: Execution (<Modifier Mode> <โหมด Type Return> <โหมดชื่อเมธอด> (<พารามิเตอร์โหมด>) <โหมดข้อยกเว้น>?) ฟังก์ชั่นการตัดจุดซึ่งสามารถตอบสนองความต้องการได้มากที่สุด
เพื่อแสดงฟังก์ชั่นของฟังก์ชั่นแทนเจนต์แต่ละจุดตอนนี้มีการเพิ่มคลาสใหม่ strutil แล้วคลาสมีดังนี้:
แพ็คเกจ com.zhangguo.spring052.aop03; นำเข้า org.springframework.stereotype.Component; @component ("strutil") ชั้นเรียนสาธารณะ strutil {public void show () {system.out.println ("สวัสดี strutil!"); -รหัสทดสอบมีดังนี้:
แพ็คเกจ com.zhangguo.spring052.aop03; นำเข้า org.springframework.context.applicationContext; นำเข้า org.springframework.context.support.classpathxmlapplicationcontext; การทดสอบระดับสาธารณะ classpathxmlapplicationContext ("AOP03.xml"); imath math = ctx.getBean ("คณิตศาสตร์", math.class); int n1 = 100, n2 = 5; Math.Add (N1, N2); Math.sub (N1, N2); Math.Mut (N1, N2); Math.div (N1, N2); strutil strutil = ctx.getBean ("strutil", strutil.class); strutil.show (); -3.1. การดำเนินการของฟังก์ชั่นการตัดจุดคำจำกัดความของการแจ้งเตือนและส่วนมีดังนี้:
แพ็คเกจ com.zhangguo.spring052.aop03; นำเข้า org.aspectj.lang.joinpoint; นำเข้า org.aspectj.lang.annotation.aspect; นำเข้า org.aspectj.lang.annotation.aspect; คลาส, cross-cut logic**/@component@appectpublic คลาส advices {@before ("การดำเนินการ (* com.zhangguo.spring052.aop03.math.* (.. ))") โมฆะสาธารณะก่อน System.out.println (jp.getSignature (). getName ()); } // ฟังก์ชั่นการดำเนินการจุดตัด // วิธีการทั้งหมดของคลาสทั้งหมดภายใต้ com.zhangguo.spring052.aop03 ถูกตัดเป็น @after ("การดำเนินการ (*com.zhangguo.spring052.aop03.*.*(.. )) -ผลการดำเนินการมีดังนี้:
Execution (<Modifier Mode> <Return Type Mode> <Method Name Mode> (<Parameter Mode>) <โหมด Exception>?)
3.2. ฟังก์ชั่นจุดตัดภายใน
// ภายในฟังก์ชั่นการตัดจุด // com.zhangguo.spring052.aop03 แพ็คเกจวิธีการทั้งหมดของคลาสทั้งหมดจะถูกตัดเป็น @After ("ภายใน (com.zhangguo.spring052.aop03.*)") โมฆะสาธารณะ System.out.println ("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------3.3. ฟังก์ชั่นจุดตัดนี้
// ฟังก์ชั่นการตัดจุดนี้ // ใช้จุดเชื่อมต่อใด ๆ ของวัตถุพร็อกซีของอินเตอร์เฟส imath @after ("นี่ (com.zhangguo.spring052.aop03.imath)") โมฆะสาธารณะหลังจาก (Joinpoint JP) { System.out.println ("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -3.4. ฟังก์ชั่น args จุดตัด
// args ฟังก์ชั่นการตัดจุด // ต้องการให้วิธีการมีการอ้างอิงสองประเภท int ก่อนที่มันจะถูกทอเป็นตรรกะการตัดข้าม @after ("args (int, int)") โมฆะสาธารณะหลังจาก (JoinPoint JP) { System.out.println ("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ - หากประเภทพารามิเตอร์ไม่ใช่ประเภทข้อมูลพื้นฐานจำเป็นต้องใช้ชื่อแพ็คเกจ
3.5. @NANNOTATION FUNCTION
แรกปรับแต่งคำอธิบายประกอบที่สามารถใส่คำอธิบายประกอบได้ในวิธีการนี้
แพ็คเกจ com.zhangguo.spring052.aop03; นำเข้า java.lang.annotation.documented; นำเข้า java.lang.annotation.elementtype; นำเข้า java.lang.annotation.retention; java.lang.annotation.target; @target ({emlementtype.method})@retention(retentionpolicy.runtime)@documentedPublic @interface myanno {} //@ennotation point-cut function ตรรกะการตัดขวาง @After (" @คำอธิบายประกอบ (com.zhangguo.spring052.aop03.myanno)") โมฆะสาธารณะหลังจาก (joinpoint jp) {system.out.println ("------------------------------------------ - แพ็คเกจ com.zhangguo.spring052.aop03; นำเข้า org.springframework.stereotype.Component; @component ("strutil") Strutil ระดับมหาชน {@myanno public void show () {system.out.println ("Hello Strutil!"); -ผลการทำงาน:
ฟังก์ชั่นการตัดจุดอื่น ๆ ด้วย @ เป็นคำอธิบายประกอบ
4. หมายเหตุการแจ้งเตือน
มีหมายเหตุประกอบการแจ้งเตือน 6 รายการใน ASPECTJ, 5 มักใช้กันทั่วไปและใช้การแนะนำน้อยลง
ก่อนอื่นแก้ปัญหาของการกำหนดจุดแทนเจนต์ Point Tangent ดังที่แสดงในรหัสต่อไปนี้เนื้อหาของฟังก์ชั่น Tangent Point จะเหมือนกัน:
แพ็คเกจ com.zhangguo.spring052.aop04; นำเข้า org.aspectj.lang.joinpoint; นำเข้า org.aspectj.lang.annotation.aspect; นำเข้า org.aspectj.lang.annotation.aspect; คลาส, cross-cut logic**/@component@appectpublic คลาส advices {@before ("การดำเนินการ (* com.zhangguo.spring052.aop04.math.* (.. ))") โมฆะสาธารณะก่อน (Joinpoint JP) { System.out.println ("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -ก่อนอื่นคุณสามารถกำหนดแทนเจนต์แล้วมัลติเพล็กซ์ได้ดังนี้:
แพ็คเกจ com.zhangguo.spring052.aop04; นำเข้า org.aspectj.lang.joinpoint; นำเข้า org.aspectj.lang.annotation.aspect; นำเข้า org.aspectj.lang.annotation.before; คลาสการแจ้งเตือน, ลอจิกแบบตัดข้าม*/@component@appectpublic class advices {// 切点 @pointcut ("การดำเนินการ (*com.zhangguo.spring052.aop04.math.*(.. ))") Public Void Pointcut () {} System.out.println ("---------- 前置通知 ----------"); System.out.println (jp.getSignature (). getName ()); } @after ("pointcut ()") โมฆะสาธารณะหลังจาก (joinpoint jp) {system.out.println ("-----------------------------------"); -แก้ไขไฟล์ advices.java และเพิ่มประเภทการแจ้งเตือนต่างๆดังนี้:
แพ็คเกจ com.zhangguo.spring052.aop04; นำเข้า org.aspectj.lang.joinpoint; นำเข้า org.aspectj.lang.proceedingjoinpoint; นำเข้า org.aspectj.lang.annotation.ather; org.aspectj.lang.annotation.around; นำเข้า org.aspectj.lang.annotation.around; นำเข้า org.aspectj.lang.annotation.aspect; นำเข้า org.aspectj.lang.annotation.before; คลาส, ลอจิกแบบตัดข้าม*/ @component @appectpublic คลาส advices {// cut point @pointcut ("Execution (*com.zhangguo.spring052.aop04.math.a*(.. ))") Public Pointcut () {} // pre-notice System.out.println (jp.getSignature (). getName ()); System.out.println ("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - throwable {system.out.println (pjp.getsignature (). getName ()); System.out.println ("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- การส่งคืน (joinpoint jp, ผลลัพธ์ของวัตถุ) {system.out.println (jp.getSignature (). getName ()); System.out.println ("ผลลัพธ์คือ:"+ผลลัพธ์); System.out.println ("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - System.out.println (jp.getSignature (). getName ()); System.out.println ("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -ผลการทำงาน:
5. การกำหนดค่าศูนย์เพื่อใช้สปริง IOC และ AOP
เพื่อให้ได้การกำหนดค่าศูนย์ตามตัวอย่างดั้งเดิมเราเพิ่มคลาสเหมือนผู้ใช้ใหม่ดังที่แสดงด้านล่าง:
แพ็คเกจ com.zhangguo.spring052.aop05; ผู้ใช้ระดับสาธารณะ {โมฆะสาธารณะแสดง () {system.out.println ("วัตถุผู้ใช้"); -คลาสนี้ไม่ได้มีคำอธิบายประกอบและคอนเทนเนอร์จะไม่ได้รับการจัดการโดยอัตโนมัติ เนื่องจากไม่มีไฟล์การกำหนดค่า XML ให้ใช้ไฟล์เป็นข้อมูลการกำหนดค่าและไฟล์ ApplicationCFG.java มีดังนี้:
แพ็คเกจ com.zhangguo.spring052.aop05; นำเข้า org.springframework.context.annotation.bean; นำเข้า org.springframework.context.annotation.componentscan; นำเข้า org.springframework.context.annotation.configuration; org.springframework.context.annotation.enableaspectjautoproxy; @configuration // คลาสการกำหนดค่าที่ใช้เพื่อแสดงคลาสปัจจุบันเป็นภาชนะที่คล้ายกับ <beans/>@componentscan (basepackages = "com.zhangguo.spring052.aop05") <บริบท: Component-scan/>@enableaspectJautOproxy (proxytargetClass = true) // พร็อกซีอัตโนมัติเทียบเท่ากับ <aop: Aspectj-autopproxy proxy-target-class = "true"> </aop id = getUser/> @Bean ผู้ใช้สาธารณะ getUser () {ส่งคืนผู้ใช้ใหม่ (); -แต่ละส่วนของคลาสนี้มีความสัมพันธ์แบบหนึ่งต่อหนึ่งกับการกำหนดค่า XML โปรดดูความคิดเห็น สิ่งนี้สะดวกกว่าการเขียน XML แต่ก็ไม่สะดวกที่จะแก้ไขหลังจากเผยแพร่ รหัสทดสอบมีดังนี้:
แพ็คเกจ com.zhangguo.spring052.aop05; นำเข้า org.springframework.context.applicationContext; นำเข้า org.springframework.context.annotation.annotationconfigaplicationcontext; นำเข้า org.springframework.context.support args) {// เริ่มต้นคอนเทนเนอร์ผ่านคลาส ApplicationContext CTX = ใหม่ AnnotationConfigapplicationContext (ApplicationCfg.Class); คณิตศาสตร์คณิตศาสตร์ = ctx.getBean ("คณิตศาสตร์", math.class); int n1 = 100, n2 = 0; Math.Add (N1, N2); Math.sub (N1, N2); Math.Mut (N1, N2); ลอง {math.div (n1, n2); } catch (exception e) {} user user = ctx.getBean ("getUser", user.class); user.show (); - advices.java เหมือนกับข้างต้นโดยไม่มีการเปลี่ยนแปลงใด ๆ ผลการทำงานมีดังนี้:
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น