0

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

         <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>

Aspect

@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) {
    // some code
    }
}

Configuration

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

Annotation

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

EDIT

Lib Project

com.acme
        |------> aspect
        |------> config
        |------> and so on...

Main Project

com.acme.app
        |------> service
        |------> config
        |------> and so on...

Thanks in advance!

HienaKill
  • 101
  • 2
  • 10
  • 2
    If you could just embed an aspect in any package and force Spring to apply it without your library user's explicit consent, this would be a security problem. Aspects can significantly alter an application's behaviour. You better document your library and put the tiny burden of adding a single package to the component scan upon the customer, which would be done in a minute, than to somehow try to sneak in the aspect into her application. – kriegaex Jun 01 '23 at 05:58

2 Answers2

0

As you are using @ComponentScan("com.acme.aspect") 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.

Satyajit Bhatt
  • 211
  • 1
  • 7
  • So, I will update my question but my package structure is similar to the following: Lib Project. ``` com.acme |------> aspect |------> config |------> and so on... ``` Main Project ``` com.acme.app |------> service |------> config |------> and so on... ``` My aspect project works fines when I use the afore mentioned annotations but the otherwise is not true – HienaKill May 31 '23 at 19:28
0

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

HienaKill
  • 101
  • 2
  • 10