亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot自定義maven-plugin插件整合asm代碼插樁

 更新時間:2022年02月22日 16:24:24   作者:code2roc  
本文主要介紹了SpringBoot自定義maven-plugin插件整合asm代碼插樁,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

背景

公司開發(fā)框架增加了web系統(tǒng)license授權(quán)證書校驗?zāi)K,實行一臺機器一個授權(quán)證書,初步方案是增加攔截器針對全局請求進(jìn)行攔截校驗,評估后認(rèn)為校驗方式單一,應(yīng)該增加重要工具類,業(yè)務(wù)service實現(xiàn)中每個方法的進(jìn)行校驗,因為涉及代碼量較大硬編碼工作困難,故選擇通過自定義maven插件在編譯期間進(jìn)行動態(tài)代碼插樁操作

項目配置

新建maven項目設(shè)置打包方式

<packaging>maven-plugin</packaging>

增加依賴項

?? ??? ? <!--使用doc的方式-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.maven</groupId>
? ? ? ? ? ? <artifactId>maven-plugin-api</artifactId>
? ? ? ? ? ? <version>3.5.2</version>
? ? ? ? </dependency>
?? ??? ?<!--使用注解的方式-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.maven.plugin-tools</groupId>
? ? ? ? ? ? <artifactId>maven-plugin-annotations</artifactId>
? ? ? ? ? ? <version>3.5.2</version>
? ? ? ? ? ? <scope>provided</scope>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.maven</groupId>
? ? ? ? ? ? <artifactId>maven-project</artifactId>
? ? ? ? ? ? <version>2.2.1</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.ow2.asm</groupId>
? ? ? ? ? ? <artifactId>asm</artifactId>
? ? ? ? ? ? <version>9.0</version>
? ? ? ? </dependency>
?

build內(nèi)容配置

?? ? <plugins>
? ? ? ? ? ? <plugin>
? ? ? ? ? ? ? ? <groupId>org.apache.maven.plugins</groupId>
? ? ? ? ? ? ? ? <artifactId>maven-plugin-plugin</artifactId>
? ? ? ? ? ? ? ? <version>3.5</version>
? ? ? ? ? ? </plugin>
? ? ? ? ? ? <plugin>
? ? ? ? ? ? ? ? <groupId>org.apache.maven.plugins</groupId>
? ? ? ? ? ? ? ? <artifactId>maven-compiler-plugin</artifactId>
? ? ? ? ? ? ? ? <version>3.6.1</version>
? ? ? ? ? ? ? ? <configuration>
? ? ? ? ? ? ? ? ? ? <source>1.8</source>
? ? ? ? ? ? ? ? ? ? <target>1.8</target>
? ? ? ? ? ? ? ? </configuration>
? ? ? ? ? ? </plugin>
? ? ? ? </plugins>

編譯攔截

創(chuàng)建編譯操作類FramePlugin,繼承AbstractMojo并使用Mojo注解標(biāo)注,output參數(shù)是class文件編譯后路徑

@Mojo(name = "deepcompile", defaultPhase = LifecyclePhase.COMPILE)
public class FramePlugin extends AbstractMojo {
    @Parameter(name = "output", defaultValue = "${project.build.directory}")
    private File output;
    public void execute() throws MojoExecutionException {
        File f = ;
        if (!f.exists()) {
            f.mkdirs();
        }
        try {
             insertPile(f);
        } catch (Exception e) {
            exceptioncount++;
            e.printStackTrace();
        }
    }

 ASM插樁

新建ClassVisitor重寫visitMethod方法來過濾訪問需要插樁的方法,需要排除自帶的init方法

public class MethodCoverageClassVisitor extends ClassVisitor {
? ? public MethodCoverageClassVisitor(ClassVisitor classVisitor) {
? ? ? ? super(Opcodes.ASM9, classVisitor);
? ? }

? ? @Override
? ? public MethodVisitor visitMethod(int access, String name, String descriptor, String signature,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?String[] exceptions) {
? ? ? ? final MethodVisitor methodVisitor = super.visitMethod(access, name, descriptor, signature, exceptions);
? ? ? ? if (name.equals("<init>")) {
? ? ? ? ? ? return methodVisitor;
? ? ? ? }
? ? ? ? return new MethodCoverageMethodVisitor(Opcodes.ASM9, methodVisitor);
? ? }
}

新建MethodVisitor重寫visitCode方法針對方法內(nèi)部字節(jié)碼進(jìn)行自定義操作,這里是使用框架內(nèi)部封裝好的一個靜態(tài)方法來校驗license證書

public class MethodCoverageMethodVisitor extends MethodVisitor {
? ? public MethodCoverageMethodVisitor(int api, MethodVisitor methodVisitor) {
? ? ? ? super(api, methodVisitor);
? ? }

? ? @Override
? ? public void visitCode() {
? ? ? ? mv.visitFieldInsn(Opcodes.INVOKESTATIC, "com/xxxx/frame/common/utils/ComplieSDK", "checkLicense", "()V");
? ? }
}

最后在execute中進(jìn)行文件遞歸查找調(diào)用,就是將已經(jīng)編譯的class文件讀取/自定義操作后保存

?private void insertPile(File root) throws IOException {
? ? ? ? if (root.isDirectory()) {
? ? ? ? ? ? for (File file : root.listFiles()) {
? ? ? ? ? ? ? ? insertPile(file);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? String className = root.getName().replace(".class", "");
? ? ? ? if (root.getName().endsWith(".class")) {
? ? ? ? ? ? //class篩選
? ? ? ? ? ? boolean flag = false;
? ? ? ? ??? ?//自定義的class文件篩選條件代碼
? ? ? ? ? ? if (flag) {
? ? ? ? ? ? ? ? System.out.println("【insertPile】:" + className);
? ? ? ? ? ? ? ? FileOutputStream fos = null;
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? final byte[] instrumentBytes = doInsertPile(root);
? ? ? ? ? ? ? ? ? ? fos = new FileOutputStream(root);
? ? ? ? ? ? ? ? ? ? fos.write(instrumentBytes);
? ? ? ? ? ? ? ? ? ? fos.flush();
? ? ? ? ? ? ? ? } catch (MojoExecutionException e) {
? ? ? ? ? ? ? ? ? ? System.out.println("【insertPile-exception】:" + className);
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? } finally {
? ? ? ? ? ? ? ? ? ? if (fos != null) {
? ? ? ? ? ? ? ? ? ? ? ? fos.close();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }

項目使用

maven-plugin項目執(zhí)行mvn install安裝到本地倉庫

框架項目配置自定義maven插件進(jìn)行打包,配置執(zhí)行的聲明周期為complie(編譯),這里goal自定義命令名稱需要和mojo注解標(biāo)注類中指定的name名稱一致

??? ??? ? <plugin>
? ? ? ? ? ? ? ? <groupId>com.xxxxx</groupId>
? ? ? ? ? ? ? ? <artifactId>frame-maven-plugin</artifactId>
? ? ? ? ? ? ? ? <version>1.2.5</version>
? ? ? ? ? ? ? ? <executions>
? ? ? ? ? ? ? ? ? ? <execution>
? ? ? ? ? ? ? ? ? ? ? ? <goals>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <!-- 執(zhí)行目標(biāo) -->
? ? ? ? ? ? ? ? ? ? ? ? ? ? <goal>deepcompile</goal>
? ? ? ? ? ? ? ? ? ? ? ? </goals>
? ? ? ? ? ? ? ? ? ? ? ? <!-- 執(zhí)行這個目標(biāo)所在的生命周期 -->
? ? ? ? ? ? ? ? ? ? ? ? <phase>compile</phase>
? ? ? ? ? ? ? ? ? ? </execution>
? ? ? ? ? ? ? ? </executions>
? ? ? ? ? ? </plugin>

到此這篇關(guān)于SpringBoot自定義maven-plugin插件整合asm代碼插樁的文章就介紹到這了,更多相關(guān)maven-plugin asm代碼插樁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Lucene實現(xiàn)多種高級搜索形式

    Lucene實現(xiàn)多種高級搜索形式

    這篇文章主要介紹了Lucene實現(xiàn)多種高級搜索形式的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Java?Dubbo服務(wù)調(diào)用擴(kuò)展點Filter使用教程

    Java?Dubbo服務(wù)調(diào)用擴(kuò)展點Filter使用教程

    Dubbo是阿里巴巴公司開源的一個高性能優(yōu)秀的服務(wù)框架,使得應(yīng)用可通過高性能的RPC實現(xiàn)服務(wù)的輸出和輸入功能,可以和Spring框架無縫集成
    2022-12-12
  • 【Java IO流】字節(jié)流和字符流的實例講解

    【Java IO流】字節(jié)流和字符流的實例講解

    下面小編就為大家?guī)硪黄綣ava IO流】字節(jié)流和字符流的實例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • 簡單了解springboot中的配置文件相關(guān)知識

    簡單了解springboot中的配置文件相關(guān)知識

    這篇文章主要介紹了簡單了解springboot中的配置文件相關(guān)知識,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • 解決mybatis無法給帶有下劃線屬性賦值問題

    解決mybatis無法給帶有下劃線屬性賦值問題

    這篇文章主要介紹了解決mybatis無法給帶有下劃線屬性賦值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 解決Intellij IDEA覆蓋tomcat配置的問題

    解決Intellij IDEA覆蓋tomcat配置的問題

    分析并解決Intellij IDEA覆蓋tomcat配置的問題/解決修改server.xml無效的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友一起看看吧
    2021-02-02
  • java實現(xiàn)簡單的圖書管理系統(tǒng)

    java實現(xiàn)簡單的圖書管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)簡單的圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Java利用Netty時間輪實現(xiàn)延時任務(wù)

    Java利用Netty時間輪實現(xiàn)延時任務(wù)

    時間輪是一種可以執(zhí)行定時任務(wù)的數(shù)據(jù)結(jié)構(gòu)和算法。本文將為大家詳細(xì)講解一下Java如何利用Netty時間輪算法實現(xiàn)延時任務(wù),感興趣的小伙伴可以了解一下
    2022-08-08
  • Java編程代碼性能優(yōu)化

    Java編程代碼性能優(yōu)化

    本文介紹了 Java 代碼優(yōu)化的過程,總結(jié)了優(yōu)化 Java 程序的一些最佳實踐,分析了進(jìn)行優(yōu)化的方法,并解釋了性能提升的原因,需要的朋友可以參考下
    2015-11-11
  • spring的13個經(jīng)典面試題

    spring的13個經(jīng)典面試題

    Spring框架是一個開放源代碼的J2EE應(yīng)用程序框架,是針對bean的生命周期進(jìn)行管理的輕量級容Spring解決了開發(fā)者在J2EE開發(fā)中遇到的許多常見的問題,我們這篇文章就來了解一下spring的面試題
    2021-06-06

最新評論