Application 1 : (Contains AOP code. A Spring boot application)
LogAspect.Java
@Aspect
@Component
public class LogAspect {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Pointcut("within(@org.springframework.stereotype.Repository *)"
+ " || within(@org.springframework.stereotype.Service *)"
+ " || within(@org.springframework.web.bind.annotation.RestController *)")
public void springBeanPointcut() {}
@Pointcut("within(com.akpanda.springmain..*)")
public void applicationPackagePointcut() {}
@Around("applicationPackagePointcut() && springBeanPointcut()")
public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
CodeSignature c = (CodeSignature) joinPoint.getSignature();
log.info("Enter: {}.{}() with ParameterName[s]= {} and argumentValue[s]= {}",
joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(),
Arrays.toString(c.getParameterNames()), Arrays.toString(joinPoint.getArgs()));
}
}
pom.xml (Application 1)
<groupId>com.akpanda</groupId>
<artifactId>aop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>aop</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Application 2 : (Another spring boot application) It has a dependency to the above Application 1, added as a maven dependency
pom.xml (Application 2)
<dependency>
<groupId>com.akpanda</groupId>
<artifactId>aop</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Main class of Application 2
@SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class SpringmainApplication {
public static void main(String[] args) {
SpringApplication.run(SpringmainApplication.class, args);
}
}
Controller class of Application 2
@RestController
public class FeesController {
@Autowired
private FeesCalculator feesCalculator;
@RequestMapping(value = "/fees/caclulate", method = RequestMethod.GET)
public Fees calculateFees() {
return feesCalculator.calculateFees();
}
}
Now when i run Application 2 and hit endpoint
/fees/caclulate
, I expect the aop dependency added in maven to kick in. and the method logAround() to be called whenever any method of Application 2 is called.
If i do not add application 1 as a dependency but simply create class in application 2 containing aspects and advices it works. But in that case I have 10 different projects for which this needs to be duplicated. I want to separate the AOP code as a module (another spring boot application) and use it in many different spring boot projects as a maven dependency.