可以自动检测自定义库/依赖项中的方面而无需 @Import 和/或 @ComponentScan 吗?

huangapple go评论69阅读模式
英文:

Is it possible to auto-detect an aspect from a custom lib/dependency without @Import and/or @ComponentScan?

问题

以下是已经翻译好的部分:

我有以下情景:

  • 使用Spring Boot和Spring AOP制作的一个库项目,切面代码位于这里;
  • 一个将我的库作为依赖项导入的主项目,它也是一个Spring Boot项目。

我想知道是否可能让我的主项目在除了依赖项包含和自定义注解使用之外,无需任何进一步的配置就能自动检测来自库的切面?我已经测试过这个库并且成功了。它被导入时没有任何错误,我创建的自定义注解被识别出来,但没有使用@ComponentScan和/或@Import时它不会触发我的切面... 当我在主项目上使用它们时,它完美运行。

是否有任何方法可以让它识别/自动检测我的切面,而不使用这些注解?

现在是库项目的一些代码:

pom

         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>2.2.8.RELEASE</version>
        </dependency>

切面

@Component
@Aspect
public class MyAspect {
    @Pointcut("@within(org.springframework.stereotype.Service)")
    public void serviceClass() {}

    @Around("@annotation(mylog) && serviceClass()")
    public void aroundExecution(ProceedingJoinPoint proceedingJoinPoint,
                                             MyLog mylog) {
    // 一些代码
    }
}

配置

@Configuration
@ComponentScan("com.acme.aspect")
@EnableAspectJAutoProxy
public class AopConfig {
} 

注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyLog {}

编辑

库项目

com.acme
        |------> aspect
        |------> config
        |------> 等等...

主项目

com.acme.app
        |------> service
        |------> config
        |------> 等等...

提前感谢!

英文:

I have the following scenario:

  • A lib project made with Spring Boot and Spring AOP, aspect code goes here;
  • A main project that will import my lib as a dependency, it's also a Spring Boot project.

I'll like to know if it's possible for my main project to auto-detect the aspect from lib without any further configuration besides dependency inclusion and custom annotation usage?
I've already tested the lib and it was succesfull.
It was imported without any error, the custom annotation I've created was recognized but it does not trigger my aspect without @ComponentScan and/or @Import addition...
When I have them on my main project, it works as a charm

Is there any way to make it recognize/auto-detect my aspect, without using those annotations?

Now some code from lib project:

pom

         &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-aop&lt;/artifactId&gt;
            &lt;version&gt;2.2.8.RELEASE&lt;/version&gt;
        &lt;/dependency&gt;

Aspect

@Component
@Aspect
public class MyAspect {
    @Pointcut(&quot;@within(org.springframework.stereotype.Service)&quot;)
    public void serviceClass() {}

    @Around(&quot;@annotation(mylog) &amp;&amp; serviceClass()&quot;)
    public void aroundExecution(ProceedingJoinPoint proceedingJoinPoint,
                                             MyLog mylog) {
    // some code
    }
}

Configuration

@Configuration
@ComponentScan(&quot;com.acme.aspect&quot;)
@EnableAspectJAutoProxy
public class AopConfig {
} 

Annotation

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyLog {}

EDIT

Lib Project

com.acme
        |------&gt; aspect
        |------&gt; config
        |------&gt; and so on...

Main Project

com.acme.app
        |------&gt; service
        |------&gt; config
        |------&gt; and so on...

Thanks in advance!

答案1

得分: 0

你正在使用 @ComponentScan("com.acme.aspect"),所以你的 MyAspect 类应该位于 com.acme.aspect 包中。因为 base-package 意味着它将扫描所有基础包中的类。基础包意味着主要包中的所有类都应该位于该包中。

英文:

As you are using @ComponentScan(&quot;com.acme.aspect&quot;) then your MyAspect class should be in com.acme.aspect package. Because base-package means it will scan all the base package classes. Base package means main package all the classes should be in that package.

答案2

得分: 0

我会选择@kriegaex的建议。感谢支持!

英文:

I'll go with @kriegaex suggestion.
Thanks for the support!

huangapple
  • 本文由 发表于 2023年6月1日 00:35:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定