spring涓璦op涓嶇敓鏁堢殑鍑犵瑙e喅鍔炴硶
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring涓璦op涓嶇敓鏁堢殑鍑犵瑙e喅鍔炴硶相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/etc' title='etc'>etc lan object contex rac 璁板綍 exception signature
鍏堢湅涓嬭繖涓棶棰樼殑鑳屾櫙锛氬亣璁炬湁涓€涓猻pring搴旂敤锛屽紑鍙戜汉鍛樺笇鏈涜嚜瀹氫箟涓€涓敞瑙Log锛屽彲浠ュ姞鍒版寚瀹氱殑鏂规硶涓婏紝瀹炵幇鑷姩璁板綍鏃ュ織(鍏ュ弬銆佸嚭鍙傘€佸搷搴旇€楁椂杩欎簺)
package com.cnblogs.yjmyzz.springbootdemo.aspect; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Log { }
鐒跺悗鍐嶅啓涓€涓狝spect鏉ヨВ鏋愯繖涓敞瑙o紝瀵规墦浜哃og娉ㄨВ鐨勬柟娉曡繘琛屽寮哄鐞嗐€€
package com.cnblogs.yjmyzz.springbootdemo.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import java.lang.reflect.Method; @Component @Aspect public class LogAspect { @Pointcut("execution (* com.cnblogs.yjmyzz.springbootdemo.service..*.*(..))") public void logPointcut() { } @Around("logPointcut()") public void around(JoinPoint point) { String methodName = point.getSignature().getName(); Object[] args = point.getArgs(); Class<?>[] argTypes = new Class[point.getArgs().length]; for (int i = 0; i < args.length; i++) { argTypes[i] = args[i].getClass(); } Method method = null; try { method = point.getTarget().getClass().getMethod(methodName, argTypes); } catch (Exception e) { e.printStackTrace(); } //鑾峰彇鏂规硶涓婄殑娉ㄨВ Log log = method.getAnnotation(Log.class); if (log != null) { //婕旂ず鏂规硶鎵ц鍓嶏紝璁板綍涓€琛屾棩蹇? System.out.println("before:" + methodName); } try { //鎵ц鏂规硶 ((ProceedingJoinPoint) point).proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } finally { if (log != null) { //婕旂ず鏂规硶鎵ц鍚庯紝璁板綍涓€琛屾棩蹇? System.out.println("after:" + methodName); } } } }
鍐欎竴涓祴璇昐ervice绫伙細
package com.cnblogs.yjmyzz.springbootdemo.service; import com.cnblogs.yjmyzz.springbootdemo.aspect.Log; import org.springframework.stereotype.Component; @Component public class HelloService { @Log public void sayHi(String msg) { System.out.println(" sayHi:" + msg); } public void anotherSayHi(String msg) { this.sayHi(msg); } }
鏈€鍚庢潵璺戜竴鎶婏細
package com.cnblogs.yjmyzz.springbootdemo; import com.cnblogs.yjmyzz.springbootdemo.service.HelloService; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; /** * @author 鑿╂彁鏍戜笅鐨勬潹杩? */ @ComponentScan("com.cnblogs.yjmyzz") @Configuration @EnableAspectJAutoProxy public class SampleApplication { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SampleApplication.class); HelloService helloService = context.getBean(HelloService.class); helloService.sayHi("hi-1"); System.out.println(" "); helloService.anotherSayHi("hi-2"); } }
杈撳嚭濡備笅锛?/p>
鏄剧劧HelloService涓殑anotherSayHi鏂规硶锛屽苟鏈aop澧炲己銆?鍘熷洜鍏跺疄寰堢畝鍗曪紝浜嗚ВAOP鍘熺悊鐨勫悓瀛︽兂蹇呴兘鐭ラ亾锛孉OP鐨勫疄鐜版湁浜岀被锛屽鏋滄槸鍩轰簬鎺ュ彛鐨勶紝浼氶噰鐢ㄥ姩鎬佷唬鐞嗭紝鐢熸垚涓€涓唬鐞嗙被锛屽鏋滄槸鍩轰簬绫荤殑锛屼細閲囩敤CGLib鐢熸垚瀛愮被锛岀劧鍚庡湪瀛愮被涓墿灞曠埗绫讳腑鐨勬柟娉曘€?/p>
鏈枃涓璈elloService骞朵笉鏄竴涓帴鍙o紝鎵€浠ヤ粠涓婂浘鐨勬柇鐐逛腑鍙互鐪嬪嚭锛屽綋Spring杩愯鏃讹紝HelloService琚鍔犱负...EnhancerBySpringCGLib...銆備絾鏄綋璋冪敤鍒癮notherSayHi鏃?/p>
鏂规硶鐨勮皟鐢ㄦ柟锛屽叾瀹炴槸鍘熷鐨凥elloSerfvice瀹炰緥锛屽嵆锛氭槸鏈粡杩嘢pring AOP澧炲己鐨勫璞″疄渚嬨€傛墍浠ヨВ鍐抽棶棰樼殑鎬濊矾灏辨湁浜嗭紝鎯冲姙娉曠敤澧炲己鍚庣殑HelloService瀹炰緥鏉ヨ皟鐢紒
鏂规硶涓€锛氱敤Autowired 娉ㄥ叆鑷韩鐨勫疄渚?/p>
杩欎釜鏂规硶锛岀涓€鐪肩湅涓婂幓鎰熻鏈変簺鎬紝鑷繁娉ㄥ叆鑷繁锛屾劅瑙夋湁鐐硅薄閫掑綊/姝诲惊鐜殑鎼炴硶锛屼絾纭疄鍙互work锛孲pring鍦ㄨВ鍐冲惊鐜緷璧栦笂鏈夎嚜宸辩殑澶勭悊鏂瑰紡锛岄伩鍏嶄簡姝诲惊鐜€?nbsp;
鏂规硶浜?/span>锛氫粠Spring涓婁笅鏂囪幏鍙栧寮哄悗鐨勫疄渚嬪紩鐢?/p>
鍘熺悊涓庢柟娉曚竴鍏跺疄绫讳技锛屼笉澶氳В閲娿€?/p>
鏂规硶涓?/span>: 鍒╃敤AopContext 涓嶈繃杩欎釜鏂规硶瑕佹敞鎰忕殑鏄紝涓荤被鍏ュ彛涓婏紝蹇呴』鍔犱笂exporseProxy=true锛屽弬鑰冧笅鍥撅細 鏈€鍚庢潵楠岃瘉涓嬭繖3绉嶆柟娉曟槸鍚︾敓鏁堬細 浠庤繍琛岀粨鏋滀笂鐪嬶紝3绉嶆柟娉曢兘鍙互瑙e喅杩欎釜闂銆?nbsp;銆€ 以上是关于spring涓璦op涓嶇敓鏁堢殑鍑犵瑙e喅鍔炴硶的主要内容,如果未能解决你的问题,请参考以下文章 eclipse鍑虹幇 the selection cannot be run on any server 瑙e喅鍔炴硶 Uncaught Error: Call to undefined function mcrypt_get_iv_size() 瑙e喅鍔炴硶 vista x64 vs2010 win32娣诲姞璧勬簮 鏈兘瀹屾垚鎿嶄綔瑙e喅鍔炴硶