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>

鎶€鏈浘鐗? src=

鏄剧劧HelloService涓殑anotherSayHi鏂规硶锛屽苟鏈aop澧炲己銆?鍘熷洜鍏跺疄寰堢畝鍗曪紝浜嗚ВAOP鍘熺悊鐨勫悓瀛︽兂蹇呴兘鐭ラ亾锛孉OP鐨勫疄鐜版湁浜岀被锛屽鏋滄槸鍩轰簬鎺ュ彛鐨勶紝浼氶噰鐢ㄥ姩鎬佷唬鐞嗭紝鐢熸垚涓€涓唬鐞嗙被锛屽鏋滄槸鍩轰簬绫荤殑锛屼細閲囩敤CGLib鐢熸垚瀛愮被锛岀劧鍚庡湪瀛愮被涓墿灞曠埗绫讳腑鐨勬柟娉曘€?/p>

鎶€鏈浘鐗? src=

鏈枃涓璈elloService骞朵笉鏄竴涓帴鍙o紝鎵€浠ヤ粠涓婂浘鐨勬柇鐐逛腑鍙互鐪嬪嚭锛屽綋Spring杩愯鏃讹紝HelloService琚鍔犱负...EnhancerBySpringCGLib...銆備絾鏄綋璋冪敤鍒癮notherSayHi鏃?/p>

鎶€鏈浘鐗? src=

鏂规硶鐨勮皟鐢ㄦ柟锛屽叾瀹炴槸鍘熷鐨凥elloSerfvice瀹炰緥锛屽嵆锛氭槸鏈粡杩嘢pring AOP澧炲己鐨勫璞″疄渚嬨€傛墍浠ヨВ鍐抽棶棰樼殑鎬濊矾灏辨湁浜嗭紝鎯冲姙娉曠敤澧炲己鍚庣殑HelloService瀹炰緥鏉ヨ皟鐢紒

 

鏂规硶涓€锛氱敤Autowired 娉ㄥ叆鑷韩鐨勫疄渚?/p>

鎶€鏈浘鐗? src=

 

杩欎釜鏂规硶锛岀涓€鐪肩湅涓婂幓鎰熻鏈変簺鎬紝鑷繁娉ㄥ叆鑷繁锛屾劅瑙夋湁鐐硅薄閫掑綊/姝诲惊鐜殑鎼炴硶锛屼絾纭疄鍙互work锛孲pring鍦ㄨВ鍐冲惊鐜緷璧栦笂鏈夎嚜宸辩殑澶勭悊鏂瑰紡锛岄伩鍏嶄簡姝诲惊鐜€?nbsp;

 

鏂规硶浜?/span>锛氫粠Spring涓婁笅鏂囪幏鍙栧寮哄悗鐨勫疄渚嬪紩鐢?/p>

鎶€鏈浘鐗? src=

 

 鍘熺悊涓庢柟娉曚竴鍏跺疄绫讳技锛屼笉澶氳В閲娿€?/p>

 

鏂规硶涓?/span>: 鍒╃敤AopContext

鎶€鏈浘鐗? src=

 

涓嶈繃杩欎釜鏂规硶瑕佹敞鎰忕殑鏄紝涓荤被鍏ュ彛涓婏紝蹇呴』鍔犱笂exporseProxy=true锛屽弬鑰冧笅鍥撅細

鎶€鏈浘鐗? src=

 

 

鏈€鍚庢潵楠岃瘉涓嬭繖3绉嶆柟娉曟槸鍚︾敓鏁堬細

鎶€鏈浘鐗? src=

浠庤繍琛岀粨鏋滀笂鐪嬶紝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喅鍔炴硶

python 瀹夎pytorch 鍙?瀹夎澶辫触瑙e喅鍔炴硶

zabbix4.4鍥捐〃涓枃鏄剧ず涔辩爜瑙e喅鍔炴硶

Jmeter 缂栧啓鎺ュ彛娴嬭瘯鐢ㄤ緥閬囧埌鐨勯棶棰樺強瑙e喅鍔炴硶