I am using this Spring AOP code in my Spring Boot starter project in STS. After debugging this for some time I don't see any problem with the AspectJ syntax. The Maven dependencies are generated by STS for a AOP starter project. Is there a glaring omission in this code like an annotation ? The other problem could be with the AOP starter project or with the way I try to test the code in a @PostConstruct method.
I installed AJDT but it appears STS should show AspectJ markers in the IDE on its own. Right ? I don't see the markers. What other AspectJ debugging options are included in STS ? -Xlint is what I used in Eclipse/AJDT.
StateHandler.java
public class StateHandler<EVENTTYPE extends EventType> {
private State<EVENTTYPE> state;
private Event<EVENTTYPE> event;
public StateHandler(State<EVENTTYPE> state, Event<EVENTTYPE> event) {
this.state = state;
this.event = event;
}
public void handle( Event<EVENTTYPE> event ){
state = state.handle( event );
}
public State<EVENTTYPE> getState() {
return state;
}
}
DeviceLogger .java
@Aspect
@Component
public class DeviceLogger {
private static Logger logger = Logger.getLogger("Device");
@Around("execution(* com.devicemachine.StateHandler.*(..))")
public void log() {
logger.info( "Logger" );
}
}
LoggerApplication.java
@SpringBootApplication
public class LoggerApplication {
private static Logger logger = Logger.getLogger("Device");
public static void main(String[] args) {
SpringApplication.run(LoggerApplication.class, args);
}
@PostConstruct
public void log(){
DeviceState s = DeviceState.BLOCKED;
StateHandler<DeviceEvent> sh = new StateHandler<DeviceEvent>( s,
Event.block(DeviceEvent.BLOCKED, "AuditMessage") );
sh.handle(Event.block(DeviceEvent.UNBLOCKED, "AuditMessage"));
}
}
There are 3 obvious things wrong and 1 not so obvious wrong.
Objectand use aProceedingJoinPointand callproceed()on that.@PostConstructmethod it might be that proxies aren't created yet and that nothing is being interceptedspring.aop.proxy-target-class=trueto yourapplication.properties. By default JDK Dynamic Proxies are used which are interface based.Fix Aspect
Your current aspect doesn't use a
ProceedingJoinPointand as such never does the actual method call. Next to that if you now would have a method that returns a value it would all of a sudden returnnull. As you aren't callingproceedon theProceedingJoinPoint.Create a bean to fix proxying and
@PostConstructAdd property to enable class proxies
In your
application.propertiesinsrc\main\resourcesadd the following property with a value oftrue