Aspect-oriented programming
25 questions in 7 subtopics.
Proxy-based AOP
How does Spring apply features such as@Transactional, @Async and @Cacheable to your beans?@Transactional on a private method has no effect. Why? Would it work on a protected or package-private method?What is the difference between Spring AOP's proxy model and AspectJ's model, and what are the practical consequences?A team wants to implement audit logging, tenant filtering and authorisation checks with custom aspects across all services. What guidance would you give?JDK dynamic proxies vs CGLIB
When does Spring use a JDK dynamic proxy and when does it use CGLIB, and what does Spring Boot do by default?With a CGLIB proxy, a bean's constructor logs a line. In older Spring versions it logged twice. Why, and what changed?An application fails withBeanNotOfRequiredTypeException: Bean named 'orderService' is expected to be of type 'OrderServiceImpl' but was actually of type 'jdk.proxy2.$Proxy87'. Explain and fix.Framework features that rely on runtime proxies cause recurring confusion: self-invocation, final classes, type mismatches. Over the long term, would you push teams towards more explicit code instead of proxy-based annotations?Pointcut expressions
Write pointcut expressions for: (1) all public methods in classes incom.acme.service and sub-packages, (2) methods annotated with @Audited, (3) all methods of beans whose name ends with 'Client'.Explain the difference between the within, @within, @annotation, this and target pointcut designators in Spring AOP.Your company has a shared aspect with the pointcut execution(* com.company..*.*(..)) for logging. Services see high CPU and odd behaviour. How would you redesign it?An organisation-wide audit aspect has been in place for years with package-based pointcuts. A package rename silently stopped auditing in several services. How would you prevent this class of problem?Advice types
Describe the five advice types in Spring AOP and when each runs.An@Around advice logs the method time, but every advised method now returns null. What is the bug?@Around("@annotation(Timed)")
public void time(ProceedingJoinPoint pjp) throws Throwable {
long t = System.nanoTime();
pjp.proceed();
log(t);
}
@Around advice that retries a method up to three times on a TransientDataAccessException. What must the advised method guarantee?Self-invocation problem
Why does no transaction start here?@Service
class OrderService {
public void placeAll(List<Order> orders) {
orders.forEach(this::place);
}
@Transactional
public void place(Order o) { ... }
}
AopContext.currentProxy() and AspectJ mode. Which would you choose in a code review?Self-invocation bugs with @Transactional keep reaching production across teams. What would you put in place?AspectJ weaving
What are compile-time, post-compile and load-time weaving in AspectJ, and how does Spring support load-time weaving?When would you choose AspectJ weaving over Spring's proxy-based AOP for a production system?Your company adopted AspectJ load-time weaving ten years ago for auditing and it now slows startup and blocks native images. How do you decide what to do?Ordering aspects
How do you control the order of two aspects that apply to the same method, and what does a lower order value mean?A retry aspect and@Transactional apply to the same method, but retries reuse a transaction that is already marked rollback-only. How do you fix the order?Your platform provides aspects for tracing, metrics, retries, transactions and security in shared starters. Design the ordering rules so teams get consistent behaviour.Proxies and aspects underpin many Spring features. Considering AOT compilation and native images, how do you see the long-term role of runtime proxies in Java frameworks?No question matches these choices.