AOP with Springboot, java.io.PrintStream

697 views Asked by At

built a simple springboot application with some aspects checking architecture and so on.

i try to catch every call to System.out.println() to give warning about usage so that is what i've found so far:

System.out.println() uses PrintStream so that i've tried this:

@Aspect
@Component
public class CleanCodeAspect {

    @Before("call(void java.io.PrintStream.println(String))")
    public void beforePrintlnCall() {
        System.out.println("About to make call to print Hello World");
   }

}

But with no success. The log says

The pointcutexpression call(void java.io.PrintStream.println(String)) contains unsupported pointcut primitive 'call'

A similar Aspect is working, but with execution instead of call:

@Aspect
@Component
public class BooleanServiceMonitor {

    @Before("execution(* de.fhb..*Service.*(java.lang.Boolean))")
        public void logServiceAccess() {
            System.out.println("You used a method with only one boolean parameter. "
                    + "Refactor it into 2 methods with True, False at the end.");
        }

    }
1

There are 1 answers

2
M. Deinum On BEST ANSWER

Spring uses proxies to apply AOP, spring can only proxy spring based beans. Class implementing PrintStream aren't generally spring configured beans. Next to that Spring AOP only supports a subset of the AspectJ syntax (as the message indicates) it supports (amongst others) execution and the special bean pointcuts.

If you want to use more features (i.e. the call point cut) you will have to use fullblown AspectJ with load or compile time weaving.