使用额外的切入点将Spring bean注入到AIBRJ中

zhte4eai  于 4个月前  发布在  Spring
关注(0)|答案(1)|浏览(62)

我有一个Sping Boot 项目,它已经使用了Spring AOP。对于一个新特性,需要使用cflow切入点,因此必须集成AOP J。
我成功地将Compile Time Weave方面编译到我的项目中,并且可以看到方面运行。我知道当项目有lombok时应该使用loadtime weaving,但我设法使用以下blog运行它。
下面是pom.xml中用于编译时织入的插件。

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.14.0</version>
                <configuration>
                    <complianceLevel>11</complianceLevel>
                    <source>11</source>
                    <target>11</target>
                    <showWeaveInfo>true</showWeaveInfo>
                    <verbose>true</verbose>
                    <Xlint>ignore</Xlint>
                    <encoding>UTF-8</encoding>
                </configuration>
                <executions>
                    <execution>
                        <id>default-compile</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <sources/>
                            <excludes>
                                <exclude>**/*.java</exclude>
                            </excludes>
                            <forceAjcCompile>true</forceAjcCompile>
                            <weaveDirectories>
                                <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
                            </weaveDirectories>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-testCompile</id>
                        <phase>process-test-classes</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                        <configuration>
                            <sources/>
                            <excludes>
                                <exclude>**/*.java</exclude>
                            </excludes>
                            <forceAjcCompile>true</forceAjcCompile>
                            <weaveDirectories>
                                <weaveDirectory>${project.build.directory}/test-classes</weaveDirectory>
                            </weaveDirectories>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

字符串
aspect的作用是查询带有截获的jointpoint的数据库。为此,我尝试将用@Repository注解的Spring bean注入到aspect中。由于aspect不是spring组件,我不能在aspect类中@Autowire repository。
Repository类

@Repository
public interface SampleRepository extends JpaRepository<Sample, String> {
}


方面类

@Aspect
@Slf4j
public class SampleAspect {
}


已尝试的代码为
1.在Aspect上使用@Configurable annotation使aspect成为spring组件。不工作。

@Aspect
@Configurable
@Slf4j
public class SampleAspect {
}


1.使用aspectOf函数创建Aspect的bean,并在其中设置存储库bean。不工作。

@Bean
    public SampleAspect sampleAspect(){
        SampleAspect sampleAspect = Aspects.aspectOf(SampleAspect.class);
        sampleAspect.setRepository(sampleRepository);
        return sampleAspect;
    }


在上面的两个步骤中,我都得到了错误unsupported jointpoint cflow。

Initialization of bean failed; nested exception is org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException: contains unsupported pointcut primitive 'cflow'


我在这里遗漏了什么吗?我需要向普通的aspectj注入一个bean。

sy5wg1nm

sy5wg1nm1#

将您想要@Autowire的POJO类作为@Configurable的依赖项是正确的,即使该类恰好是本机AOP J方面也是如此。然而,将本机方面设置为@Bean@Component是错误的,并且适得其反的方法,因为这将方面第二次注册为Spring AOP方面,这也解释了UnsupportedPointcutPrimitiveException ... cflow问题。
这张图中缺少的信息是,为了让@Configurable工作,你需要AnnotationBeanConfigurerAspect从类路径上的**spring-aspects。**这对于加载时和编译时编织场景都是正确的。也就是说,你希望在POM中有这样的东西:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>${spring.version}</version>
</dependency>

字符串
在ApplyJ Maven Plugin配置中,您还需要将spring-aspects声明为方面库:

<aspectLibraries>
  <aspectLibrary>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
  </aspectLibrary>
</aspectLibraries>


有关详细信息,请参阅this related answer
稍微偏离主题:我发现在后续阶段中在一个模块中使用MavenMaven和AjenJ Maven的方法,在同一个输出目录上工作,是有问题的,我建议在模块A中使用Maven Observer和Lombok构建一个未编织的模块版本,然后在模块B中将方面编织到来自A的类中。所有其他模块都应该依赖于B,而不是中介A。为了避免A成为一个可传递的依赖项,在类路径上的两个不同版本中具有相同的类,在声明B的依赖项时,给予A provided作用域可能是有意义的。

相关问题