how to retransform a class at runtime

Short Answer Don’t iterate through all the loaded classes from Instrumentation. Instead, just examine the class name passed in to the transformer and if it matches your target class, then transform it. Otherwise, simply return the passed classfileBuffer unmodified. Make the set up calls outside the transformer, (i.e. in your case, do the following from … Read more

Java Aspect-Oriented Programming with Annotations

Let’s imagine you want to log the time taken by some annoted methods using a @LogExecTime annotation. I first create an annotation LogExecTime: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface LogExecTime { } Then I define an aspect: @Component // For Spring AOP @Aspect public class LogTimeAspect { @Around(value = “@annotation(annotation)”) public Object LogExecutionTime(final ProceedingJoinPoint joinPoint, final LogExecTime … Read more

Spring AOP for non spring component

Spring AOP can only be applied to Spring-managed components/beans, not to non-Spring POJOs. If you want to apply AOP to non-Spring classes you need AspectJ, not a proxy-based “AOP lite” framework like Spring AOP. For more information about how to use AspectJ (which does not need Spring at all) in combination with Spring and how … Read more

Configuring AspectJ aspects using Spring IoC with JavaConfig?

Turns out that there is an org.aspectj.lang.Aspects class to provide for specifically this purpose. It appears that the aspectOf() method is added by the LTW which is why it works fine in XML configuration, but not at compile time. To get around this limitation, org.aspectj.lang.Aspects provides a aspectOf() method: @Bean public com.xyz.profiler.Profiler profiler() { com.xyz.profiler.Profiler … Read more

Spring AOP Advice on Annotated Controllers

It’s possible to have advice on annotated controllers. I assume you want to advice after execution of all methods in classes annotated with @Controller. Here’s an example: import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspect public class ControllerAspect { @Pointcut(“within(@org.springframework.stereotype.Controller *)”) public void controllerBean() {} @Pointcut(“execution(* *(..))”) public void methodPointcut() {} @AfterReturning(“controllerBean() && methodPointcut() “) public … Read more

Ruby dependency injection libraries

Jamis Buck, who wrote Copland and Needle, posted here about Needle, dependency injection and their usefulness in a Ruby world. It’s long but worth reading, but in case you want the single paragraph most relevant to your question, I’d suggest this one, from just before the end: DI frameworks are unnecessary. In more rigid environments, … Read more