บทความนี้แสดงให้เห็นถึงการใช้งานของ AOP โดยใช้คำอธิบายประกอบภาพและการกำหนดค่า XML ในฤดูใบไม้ผลิ
ต่อไปนี้เป็น โครงการ Java ที่ใช้ AOP โดยใช้คำอธิบายประกอบภาพรวม
ก่อนอื่นคือไฟล์ applicationcontext.xml ที่อยู่ใต้ classpath
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemalocation = "http:/ http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://ww.springframework.org/schema http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <! id = "SecurityHandler"/> </epeans>
จากนั้นก็มีอินเทอร์เฟซเลเยอร์บริการและคลาสการใช้งาน
แพ็คเกจ com.jadyer.annotation; อินเตอร์เฟสสาธารณะ userManager {โมฆะสาธารณะ adduser (ชื่อผู้ใช้สตริง, รหัสผ่านสตริง); โมฆะสาธารณะ Deluser (int userID); สตริงสาธารณะ FindUserById (int userID); โมฆะสาธารณะ modifyuser (int userId, ชื่อผู้ใช้สตริง, รหัสผ่านสตริง); } / *** UserManager ด้านบนเป็นอินเทอร์เฟซของเลเยอร์บริการ* userManagerImpl ต่อไปนี้คือคลาสการใช้งานของอินเตอร์เฟสเลเยอร์บริการ* / แพ็คเกจ com.jadyer.annotation; คลาสสาธารณะ UserManagerImpl ใช้ UserManager {Public Void Adduser (ชื่อผู้ใช้สตริง, รหัสผ่านสตริง) {System.out.println ("------- userManagerImpl.adduser () ถูกเรียกใช้ -----"); } โมฆะสาธารณะ deluser (int userId) {system.out.println ("-------- usermanagerimpl.deluser () ถูกเรียกใช้ ------"); } สตริงสาธารณะ findUserById (int userId) {system.out.println ("------ userManagerImpl.finduserById () ถูกเรียกใช้ ------"); กลับ "ชีวิตที่ต้องเผชิญกับเหล็ก"; } โมฆะสาธารณะ modifyUser (int userId, สตริงชื่อผู้ใช้, รหัสผ่านสตริง) {system.out.println ("------- userManagerImpl.modifyUser () ถูกเรียกใช้ ------"); - ถัดไปคือคลาสรายการที่ทำเครื่องหมายด้วย APISICJ
แพ็คเกจ com.jadyer.annotation; นำเข้า org.aspectj.lang.annotation. หลังจาก; นำเข้า org.aspectj.lang.annotation.aspect; นำเข้า org.aspectj.lang.annotation.pointcut; @Aspect คลาสสาธารณะ SecurityHandler { /** * กำหนด PointCut * @See PointCut ชื่อของ PointCut คือ AddaddMethod () วิธีนี้ไม่มีค่าส่งคืนและพารามิเตอร์* @see วิธีนี้เป็นตัวระบุและไม่เรียกมันว่า*/@pointcut ("การดำเนินการ (* เพิ่ม* (.. ))") // จับคู่วิธีการทั้งหมดเริ่มต้นด้วยการเพิ่มโมฆะส่วนตัว addaddMethod () {}; /*** กำหนดคำแนะนำ* @see ระบุว่าการสมัครสมาชิก PointCut คำแนะนำของเราใช้กับ ON joinPoints* // @ก่อน ("AddaddMethod ()") @after ("AddaddMethod ()") การตรวจสอบความปลอดภัยส่วนตัว () - ในที่สุดคลาสทดสอบลูกค้า
แพ็คเกจ com.jadyer.annotation; นำเข้า org.springframework.context.applicationContext; นำเข้า org.springframework.context.support.classpathxmlapplicationContext; /*** การสนับสนุนของฤดูใบไม้ผลิสำหรับ AOP: ใช้วิธีการเพิ่มความคิดเห็น* @see ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ เนื่องจากเป็นพร็อกซีไดนามิก JDK หากคุณต้องการสร้างพร็อกซีคลาสจะต้องใช้อินเทอร์เฟซ* @see หากคลาสไม่มีอินเทอร์เฟซดำเนินการและยังคงใช้การใช้งาน AOP เริ่มต้นของสปริง นั่นคือการพัฒนานิสัยของการเขียนโปรแกรมที่เน้นอินเทอร์เฟซ* @see - - เพิ่มการสนับสนุนคำอธิบายประกอบ * @see * spring_home // dist // spring.jar * @see * spring_home // lib // log4j // log4j-1.2.14.jar * @See * spring_home // lib // lib // lib //. ข้อกังวลเกี่ยวกับการแบ่งแยกและสร้าง SecurityHandler.java * @see 3 ใช้คำอธิบายประกอบเพื่อระบุ SecurityHandler เป็นแง่มุม * @See 4 ใช้คำอธิบายประกอบเพื่อกำหนดคำแนะนำและ PointCut * @See 5. เปิดใช้งานการสนับสนุนด้านการเพิ่มความคิดเห็นและกำหนดค่าคลาสเป้าหมายและคลาส - (userManager) Factory.getBean ("userManager"); userManager.adduser ("จาง Qiling", "02200059"); - ต่อไปนี้เป็น โครงการ Java ที่ใช้ AOP โดยใช้ไฟล์การกำหนดค่า XML
ก่อนอื่นคือไฟล์ ApplicationContext-cglib.xml ที่อยู่ในไดเรกทอรีรูทของ SRC
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemalocation = "http:/ http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://ww.springframework.org/schema http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <! <bean id = "userManager"/> <bean id = "SecurityHandler"/> <aop: config> <aop: ide id = "SecurityAspect" ref = "SecurityHandler"> <aop: pointcut id = "addaddMethod" Expression = "การดำเนินการ" </aop: แง่มุม> </aop: config> </epeans> <!- จับคู่วิธีการทั้งหมดเริ่มต้นด้วยการเพิ่ม (*เพิ่ม*(.. )) จับคู่วิธีการทั้งหมดของคลาสทั้งหมดภายใต้ com.jadyer.servcices.impl การดำเนินการแพ็คเกจ (com.jadyer.servcices.impl.*.*(.. .. com.jadyer.services.impl.*. เพิ่ม*(.. )) || Execution (*com.jadyer.services.impl.*. del*(.. )) ->
จากนั้นก็มีอินเทอร์เฟซเลเยอร์บริการและคลาสการใช้งาน
แพ็คเกจ com.jadyer.cglib; อินเตอร์เฟสสาธารณะ userManager {โมฆะสาธารณะ adduser (ชื่อผู้ใช้สตริง, รหัสผ่านสตริง); โมฆะสาธารณะ Deluser (int userID); สตริงสาธารณะ FindUserById (int userID); โมฆะสาธารณะ modifyuser (int userId, ชื่อผู้ใช้สตริง, รหัสผ่านสตริง); } / *** UserManager ด้านบนเป็นอินเทอร์เฟซเลเยอร์บริการ* UserManagerImpl ต่อไปนี้คือคลาสการใช้งานของอินเตอร์เฟสเลเยอร์บริการ* / แพ็คเกจ com.jadyer.cglib; คลาสสาธารณะ userManagerImpl {// ใช้ userManager {public void adduser (ชื่อผู้ใช้สตริง, รหัสผ่านสตริง) {system.out.println ("------- userManagerImpl.adduser () ถูกเรียกใช้ -----"); } โมฆะสาธารณะ deluser (int userId) {system.out.println ("-------- userManagerImpl.deluser () ถูกเรียกใช้ -----"); } สตริงสาธารณะ findUserById (int userId) {system.out.println ("------ userManagerImpl.finduserById () ถูกเรียกใช้ -----"); กลับ "จางซาน"; } โมฆะสาธารณะ modifyUser (int userId, String username, รหัสผ่านสตริง) {system.out.println ("------- userManagerImpl.modifyuser () ถูกเรียกใช้ -----"); - ถัดไปคือคลาสรายการที่ระบุใน ApplicationContext-cglib.xml
แพ็คเกจ com.jadyer.cglib; นำเข้า org.aspectj.lang.joinpoint; /*** ส่งผ่านข้อมูลการโทรของลูกค้าไปยังคำแนะนำ* @see คุณสามารถเพิ่มพารามิเตอร์ joinpoint ลงในคำแนะนำเพื่อรับชื่อวิธีและค่าพารามิเตอร์ของการโทรของลูกค้า* @see ในอนาคตมีกรณีที่ใช้ AOP เพียงอย่างหมดจดเพื่อเขียนสิ่งนี้ เราใช้ธุรกรรมที่จัดทำโดย Spring* @see เป็นหลักคุณสามารถรู้เกี่ยวกับเรื่องนี้ ต่อไปนี้เป็นรหัสตัวอย่าง */ คลาสสาธารณะ SecurityHandler {Private Void Checksecurity (JoinPoint JoinPoint) {สำหรับ (int i = 0; i <joinpoint.getargs (). ความยาว; i ++) {system.out.println (joinpoint.getargs () [i]); // รับค่าพารามิเตอร์ของวิธีการที่เรียกโดยไคลเอนต์} system.out.println (joinpoint.getSignature (). getName ()); // รับชื่อวิธีการที่เรียกโดยไคลเอนต์ System.out.println ("------ [ตรวจสอบความปลอดภัยถูกเรียกใช้] ------"); }} ในที่สุดคลาสทดสอบลูกค้า
แพ็คเกจ com.jadyer.cglib; นำเข้า org.springframework.context.applicationContext; นำเข้า org.springframework.context.support.classpathxmlapplicationContext; /** * @ดู ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- การสืบทอดเป็นการดีที่สุดที่จะไม่ใช้การประกาศขั้นสุดท้ายสำหรับคลาสเป้าหมาย* @see - - เพราะเป้าหมายที่เราพร็อกซีเป็นวัตถุทางธุรกิจโดยทั่วไป* @see - - ไคลเอนต์คลาสสาธารณะ {โมฆะสาธารณะคงที่หลัก (สตริง [] args) {ApplicationContext Factory = ใหม่ classPathxMlApplicationContext ("ApplicationContext-cglib.xml"); // เมื่อ UserManagerImpl ใช้อินเทอร์เฟซ UserManager สปริงจะใช้พร็อกซีไดนามิกของ JDK โดยอัตโนมัติ // หากโครงการได้แนะนำไลบรารี CGLIB และบังคับให้ใช้ CGLIB Proxy ในไฟล์การกำหนดค่าสปริงจะใช้ cglib proxy // usermanager usermanager = (usermanager) // เนื่องจาก UserManagerImpl ไม่ได้ใช้อินเตอร์เฟส UserManager ในเวลานี้อินเตอร์เฟส UserManager จึงไม่สามารถใช้ในประเภทรับ//ห้องสมุด CGLIB ได้รับการแนะนำในโครงการ แม้ว่า CGLIB Proxy จะไม่ถูกบังคับให้ใช้ในไฟล์กำหนดค่าสปริงจะใช้ CGLIB Proxy UserManagerImpl โดยอัตโนมัติ userManager = (userManagerImpl) Factory.getBean ("userManager"); usermanager.adduser ("Wu Sansheng", "02200059"); -ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น