I have a concrete class we can call ServiceImpl
who implements an interface called Service
. I am using Spring AOP/Aspect-J to log the method execution times, which currently works just fine on the public method in that ServiceImpl
class (which makes logical sense knowing how proxies work). One thing I would like to state right away is that I am using the swagger-codegen-maven-plugin plugin which basically generates code for you at compile time, in my case, it's generating some of my custom data types that I am using as a return type for my public entry method in my ServiceImpl
that calls the nested private methods that I am unable to log/track using SpringAOP (why I'm making this thread). Also, I am using a multi-module maven project that has this structure:
- api module (Spring Web REST Controller and where Application.java with main method is)
- model module (where the POJOs/data classes are output after generated by swaggercodegen plugin)
- service module (where my
ServiceImpl
,Service
class/interface are and where my Spring AOP Aspect/Advice/Annotations are defined.
Now, before you tell me Spring AOP can't intercept nested private methods without enabling load time weaving in Spring AOP, I found this out the hard way after trying to Self Invocation and calling the nested private methods on that self
reference (of type ServiceImpl
which was @Autowired
). I Got a Null Pointer Exception (NPE) everytime, as it seemed that reference (private ServiceImpl self
) was null no matter if I Autowired the bean or got it from the ApplicationContext (i.e. applicationContext.get(ServiceImpl.class)
)
However, my question is simply if there are any alternatives to the private methods; Is there some changes I could make to my logic that would the cleanest way to solve this problem and finally successfully log the response/execution times of both my public and nested private methods in my Spring Boot application.
Below is a pretty much how my code is defined.
@Service
public class ServiceImpl implements Service {
@MySpringAOPAnnotationThatWorks
public SwaggerGeneratedDataType1 myPublicMethod(SwaggerGeneratedDataType2 myMethodArgument) {
// ...
// I WANT TO TRACK THESE NESTED PRIVATE METHOD EXECUTION TIMES
myNestedPrivateMethod1(myMethodArgument.getSomeAttribute1()); // NULL POINTER EXCEPTION when I tried using Self Invocation and using "self" instance to call method on
// ...
// I WANT TO TRACK THESE NESTED PRIVATE METHOD EXECUTION TIMES
myNestedPrivateMethod2(myMethodArgument.getSomeAttribute2());
}
private void myNestedPrivateMethod1(String myNestedMethodArgument1) {
// some logic here
}
private void myNestedPrivateMethod2(String myNestedMethodArgument2) {
// some logic here
}
What's the easiest way to simply log those execution times of the nested private methods?