A Wrapper Object is more friendly term for the use of aspect oriented programming techniques to intercept and execute code before after or around a specific method call.
Rules;
- Should not contain what would be standard business logic.
- If removed the application should still function.
- Must not change the accepted interface of wrappee. That is, we should not pass in an argument assuming a wrapper will pick it up, but the original contract will not use it.
Examples;
Execution Profiling --
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class TimeExecutionProfiler {
private static final Logger log = LoggerFactory.getLogger(TimeExecutionProfiler.class);
@Around("SystemArchitecture.serviceOperation()")
public Object profile(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
Object output = pjp.proceed();
long elapsedTime = System.currentTimeMillis() - start;
log.info("ServicesProfiler.profile(): Method execution time: " + elapsedTime + " milliseconds.");
return output;
}
@After("SystemArchitecture.serviceOperation()")
public void profileMemory() {
log.info("JVM memory in use = "
+ ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1048576) + " MB");
}
}
User Encryption;
Commons needs for this are based on the utilization of our Crud and Query Object patterns. For example, if we want to save a User in the system using the generic Crud Object, but User has a password field which potentially needs to be encrypted, we could wrapper the Crud Object save method so that when a User is being saved, we run an encryption method. In this way, we do not have to write a separate service or dao method for creating a user. Another use case is advanced validation. Another use case is Multi-Tenant enforcement.
Further Knowledge;
This can be done through either separate compilation or via runtime load weaving. But, it is generally done at runtime unless sufficient IDE tools are available to make compilation seamless.
Spring Roo makes use of separate compilation via aspectj aj files.
Reference;
0 comments:
Post a Comment