簡體   English   中英

在春季啟動之前AspectJ啟動

[英]AspectJ before spring boot starts

假設我有一個spring boot應用程序

@SpringBootApplication
public abstract class AbstractMicroServer {

    public static void main(final String[] args) {
        // here some asppect should start
        final SpringApplication app = new SpringApplication(AbstractMicroServer.class);
        app.run(args);
    }
}

我的aspectJ類

@Aspect
public class AOP{

    @Pointcut("execution(* org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.ignoreResourceType(..)")
    public void intercept() {
    }

    @Around("intercept()")
    public Object intercept(final ProceedingJoinPoint joinPoint) throws Throwable {
        return joinPoint.getArgs();
    }
}

我希望在Spring啟動之前准備好這個Aspect。 這可能是我想做的嗎? 由於某些我不知道的原因,這方面沒有被截獲。

POM

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.12</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.12</version><!--$NO-MVN-MAN-VER$-->
        </dependency>

      <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.10</version>
                    <configuration>
                        <complianceLevel>1.8</complianceLevel>
                        <source>1.8</source>
                        <target>1.8</target>
                        <showWeaveInfo>true</showWeaveInfo>
                        <verbose>true</verbose>
                        <Xlint>ignore</Xlint>
                        <encoding>UTF-8</encoding>
                        <includes>
                            <include>**/*.java</include>
                            <include>**/*.aj</include>
                        </includes>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <!-- use this goal to weave all your main classes -->
                                <goal>compile</goal>
                                <!-- use this goal to weave all your test classes -->
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

我沒有使用彈簧靴啟動器。 以上是我用於aop的所有配置和代碼。

春天AOP

運行時編織

由於Spring框架基於代理的特性,Spring AOP基於運行時編織 這意味着目標類在Spring運行時期間變為代理。

在大多數情況下,Spring框架不會對其框架類進行代理。 您感興趣的類CommonAnnotationBeanPostProcessor未被代理。 因此,Spring AOP的無法攔截到任何電話ignoreResourceType的方法CommonAnnotationBeanPostProcessor

但是,這並不意味着你運氣不好。 您仍然可以利用AspectJ的二進制編織

AspectJ的

二元編織

在二進制編織中,目標和方面源代碼(* .java)分別編譯為二進制類(.class)。 然后將二進制類與AspectJ編譯器(ajc)編織在一起。

在您的情況下,方面源代碼( AOP.java )將使用AspectJ編譯器編譯為二進制類( AOP.class )。 AOP.class和現有的Spring類CommonAnnotationBeanPostProcessor.class將一起編織成一個新的編織CommonAnnotationBeanPostProcessor.class

編織前的代碼

下面是代碼段ignoreResourceType類的方法CommonAnnotationBeanPostProcessor

public void ignoreResourceType(String resourceType) {
        Assert.notNull(resourceType, "Ignored resource type must not be null");
        this.ignoredResourceTypes.add(resourceType);
}

編織后的代碼

現在,請注意AspectJ編寫方法后的更改。

public void ignoreResourceType(String resourceType) {
        JoinPoint var3 = Factory.makeJP(ajc$tjp_0, this, this, resourceType);
        SpringFrameworkClassAspect var10000 = SpringFrameworkClassAspect.aspectOf();
        Object[] var4 = new Object[]{this, resourceType, var3};
        var10000.adviceAround((new CommonAnnotationBeanPostProcessor$AjcClosure1(var4)).linkClosureAndJoinPoint(69648));
}

如何實現AspectJ Binary Weaving?

  • 您可以在Mojo的AspectJ Maven插件的幫助下生成二進制編織。
  • 你仍然需要你的方面課程。
  • 確保在weaveDependencies下包含spring-context依賴weaveDependencies

以下是項目pom.xml的插件部分的摘錄。 你可以在這里找到一個完整的工作示例。

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.10</version>
            <configuration>
                <showWeaveInfo/>
                <source>1.8</source>
                <target>1.8</target>
                <complianceLevel>${java.version}</complianceLevel>
                <Xlint>ignore</Xlint>
                <forceAjcCompile>true</forceAjcCompile>
                <sources/>
                <weaveDirectories>
                    <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
                </weaveDirectories>
                <weaveDependencies>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-context</artifactId>
                    </dependency>
                </weaveDependencies>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
        </plugin>

在我看來,你想攔截Spring類中的方法執行。 為此,您有兩種選擇:

  • 使用加載時間編織( -javaagent:/path/to/aspectjweaver-<version>.jar作為JVM參數)
  • 在構建時編寫 Spring庫類以創建庫的特殊編織版本( 請參閱相關的aspectj-maven-plugin文檔 ),並使用生成的weaved類而不是原始的spring jar文件。 您生成的配置看起來與此類似:

     <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <configuration> <weaveDependencies> <weaveDependency> <groupId>org.agroup</groupId> <artifactId>to-weave</artifactId> </weaveDependency> <weaveDependency> <groupId>org.anothergroup</groupId> <artifactId>gen</artifactId> </weaveDependency> </weaveDependencies> </configuration> </plugin> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM