Implementing Hot‑Pluggable AOP in Spring: Dynamic Management of Advice
This article demonstrates how to build a hot‑pluggable AOP solution in Spring by dynamically adding and removing Advice at runtime, covering the required concepts, core implementation code, a complete demo, and practical considerations for backend Java developers.
The article starts with a requirement: let end users turn logging on or off dynamically instead of hard‑coding it, and proposes using Spring AOP with a plug‑in style where Advice can be added or removed at runtime.
It then introduces the prerequisite concepts— Advice (the operation performed at a join point), Advisor (holds an Advice), and Advised (the proxy factory that manages Advice and Advisor)—providing their fully‑qualified class names.
Next, the core hot‑plug AOP logic is presented, including the dynamic management endpoint, event listener, plugin installation/uninstallation, proxy registration, and the method that adds or removes Advice. The key code fragments are shown below:
@RestControllerEndpoint(id = "proxy")
@RequiredArgsConstructor
public class ProxyMetaDefinitionControllerEndPoint {
private final ProxyMetaDefinitionRepository proxyMetaDefinitionRepository;
@GetMapping("listMeta")
public List<ProxyMetaDefinition> getProxyMetaDefinitions(){
return proxyMetaDefinitionRepository.getProxyMetaDefinitions();
}
// ... other CRUD methods ...
} @RequiredArgsConstructor
public class ProxyMetaDefinitionChangeListener {
private final AopPluginFactory aopPluginFactory;
@EventListener
public void listener(ProxyMetaDefinitionChangeEvent event){
ProxyMetaInfo info = aopPluginFactory.getProxyMetaInfo(event.getProxyMetaDefinition());
switch (event.getOperateEventEnum()){
case ADD: aopPluginFactory.installPlugin(info); break;
case DEL: aopPluginFactory.uninstallPlugin(info.getId()); break;
}
}
} public static void registerProxy(DefaultListableBeanFactory beanFactory, ProxyMetaInfo meta){
AspectJExpressionPointcutAdvisor advisor = getAspectJExpressionPointcutAdvisor(beanFactory, meta);
addOrDelAdvice(beanFactory, OperateEventEnum.ADD, advisor);
}
// ... getAspectJExpressionPointcutAdvisor and addOrDelAdvice implementations ...A complete demonstration follows: a HelloService implementing BeanNameAware and BeanFactoryAware , a HelloController exposing a REST endpoint, and a logging interceptor LogMethodInterceptor that logs method invocation details.
@Slf4j
public class LogMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object result;
try { result = invocation.proceed(); }
finally { log.info("TargetClass:{}, method:{}, args:{}",
invocation.getThis().getClass().getName(),
invocation.getMethod().getName(),
Arrays.toString(invocation.getArguments())); }
return result;
}
}Two test scenarios are described. In the first, no Advice is added and the endpoint returns only the service result. In the second, a POST request to the dynamic endpoint adds the logging Advice, which is verified by console output showing the interceptor’s log messages; a subsequent DELETE request removes the Advice, and the logs disappear, confirming successful hot‑plugging.
The conclusion emphasizes that mastering the concepts of Advice, Advisor, Advised, and Pointcut, as well as understanding class‑loader mechanics, enables developers to replace Spring’s automatic AOP handling with a custom, dynamically controllable solution, and notes that alternative implementations (e.g., using TargetSource ) are also possible.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.