I have searched on it but didnt get my required answer about including scan base package and excluding a sub package.
eg: project structure
MyTest
--src/main/java
----com.spring
----com.spring.controller
----com.spring.entity
----com.spring.repository
----com.spring.service
----com.spring.service.dto
----com.spring.repo.dto
----com.spring.repo.dto.v2
----com.spring.service.dto.v2
----com.spring.service.pojo
I want to write a pointcut where I will scan base package com.spring
and want to exclude dto
sub package.
here is what I tried:
basePkg="com.spring";
"execution(* " basePkg ".service.*.*(..))" " && !execution(* *.dto.*(..))" ;
this is excluding dto but only scanning service pkg. NB: I dont want to include all required pkg manually as there might be(actually is) numbers of pkgs in the project.
"execution(* " basePkg "..*.*(..))" " && !execution(* *.dto.*(..))" ;
"execution(* " basePkg "..*.*(..))" " && !execution(* " basePkg "..*.dto.*.*(..))"
above I scanned all package and tried excluding dto sub package...but its scanning all package no dto exclusion.
Here I need some guidance.
EDIT:
generally spring app has a base pkg and adding business in corresponding sub-pkgs. Here I gave com.spring. So I need all methods to be advised in its all sub pkgs AND if I want to exclude any sub pkg(com.spring.dto/com.spring.service.dto/com.spring.service.dto.v2/com.spring.repo.dto/com.spring.repo.dto.v2 etc) I should be able to exclude those as its all have dto sub-pkg common.
PLEASE refer my project pkg. I need a generic one to add or remove not individual sub to sub pkgs
CodePudding user response:
Your project structure
com.spring
|- service
| |- Service.java
|
|- dto
| |- Dto.java
This is how you can exclude dto from the poincut
@Pointcut("within(com.spring.service..*) && !within(com.spring.service.dto..*)")
public void serviceMethodPointcut() {}
For the project structure above in the question
MyTest
--src/main/java
----com.spring
----com.spring.controller
----com.spring.entity
----com.spring.repository
----com.spring.service
----com.spring.service.dto
----com.spring.repo.dto
----com.spring.repo.dto.v2
----com.spring.service.dto.v2
----com.spring.service.pojo
This pointcut will work -
execution(* com.spring..*(..)) && within(com.spring..*) && !within(*..dto..*);
OR
within(com.spring..*) && !within(*..dto..*)
As, the above pointcut matches any method in com.spring package and its subpackages except the packages have dto in there name.
example of the method matched with the pointcut
com.spring.service.myservice.methodB()
com.spring.pojo.mypojo.methodA()
example of the method will not be matched with the pointcut
com.spring.service.dto.mydto.methodA()
com.spring.repo.dto.v2.methodA()