"Can you explain how Spring IoC and AOP work?"
Spring is the backbone of enterprise Java development. Understanding IoC and AOP demonstrates that you grasp the framework at an architectural level, not just as a user of annotations. This question is standard at any company using Java for backend services.
Define IoC (Inversion of Control): instead of objects creating their own dependencies, the Spring container manages object creation, configuration, and lifecycle. Dependencies are injected rather than looked up.
Explain the IoC container: BeanFactory is the basic container; ApplicationContext extends it with enterprise features. Beans are defined via XML, annotations (@Component, @Service), or Java config (@Configuration + @Bean).
Describe the bean lifecycle: instantiation → property population → BeanNameAware/BeanFactoryAware → BeanPostProcessor.postProcessBeforeInitialization → @PostConstruct/InitializingBean → BeanPostProcessor.postProcessAfterInitialization → ready → @PreDestroy/DisposableBean.
Define AOP (Aspect-Oriented Programming): separates cross-cutting concerns (logging, transactions, security) from business logic using proxies.
Explain AOP implementation: Spring uses JDK dynamic proxies for interfaces and CGLIB proxies for classes. Key concepts: Aspect, Advice (Before, After, Around), Pointcut, JoinPoint.
Spring IoC, or Inversion of Control, is a design principle where the framework manages object creation and dependency injection instead of the application code. The Spring container (ApplicationContext) reads configuration — whether from XML, annotations like @Component and @Autowired, or Java @Configuration classes — and creates a dependency graph of beans. When a bean needs a dependency, the container injects it via constructor, setter, or field injection. The bean lifecycle includes instantiation, dependency injection, various aware interface callbacks, BeanPostProcessor hooks, initialization (@PostConstruct), and finally destruction (@PreDestroy). This lifecycle is what enables AOP to work. Spring AOP uses the proxy pattern to add behavior to beans without modifying their code. When you annotate a method with @Transactional, for example, Spring wraps the bean in a proxy. For beans implementing interfaces, Spring uses JDK dynamic proxies; for concrete classes, it uses CGLIB to generate a subclass at runtime. The proxy intercepts method calls and executes advice — @Before runs before the method, @After runs after, and @Around wraps the entire execution with custom logic. The pointcut expression defines which methods the advice applies to. Internally, Spring AOP is built on the BeanPostProcessor mechanism — AbstractAutoProxyCreator checks each bean during initialization and wraps it in a proxy if any advisor matches. This is why self-invocation bypasses AOP — calling this.method() doesn't go through the proxy.
Mention the self-invocation problem with AOP — it shows deep understanding and is a common follow-up.
Know the difference between JDK dynamic proxy and CGLIB — when each is used and why.
Be ready to explain @Transactional as a concrete example of AOP in action.
Use HireMe AI to get real-time guidance on bean lifecycle steps if you lose track during the interview.
BeanFactory is the basic container with lazy initialization. ApplicationContext extends it with eager initialization, event publishing, internationalization, and AOP support.
Spring AOP uses proxies. JDK proxies work through interfaces (no private methods), and CGLIB creates subclasses (cannot override private methods). So the proxy cannot intercept private method calls.
When Bean A depends on Bean B and vice versa. Spring resolves this for singleton beans using a three-level cache (singletonObjects, earlySingletonObjects, singletonFactories) that exposes early references before full initialization.
HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.
Try HireMe AI Free