Spring Interview Questions and Answers: IoC, AOP, Bean Lifecycle, Circular Dependency, Transactions, Annotations, MVC Flow and Thread Concurrency

This article provides comprehensive explanations of Spring framework concepts—including IoC/AOP principles, bean lifecycle, circular dependency resolution, transaction pitfalls, annotation configuration, MVC processing flow, advice types, and thread‑safety mechanisms—along with illustrative code snippets for interview preparation.

IT Xianyu
IT Xianyu
IT Xianyu
Spring Interview Questions and Answers: IoC, AOP, Bean Lifecycle, Circular Dependency, Transactions, Annotations, MVC Flow and Thread Concurrency

Inversion of Control (IoC) and Aspect‑Oriented Programming (AOP) are core principles of the Spring framework; IoC relies on dependency injection while AOP uses dynamic proxies (JDK or CGLIB) to separate cross‑cutting concerns such as logging, security and transactions.

The Spring bean lifecycle consists of three main steps—createBeanInstance, populateBean, and initializeBean—during which the container may expose early references to resolve setter‑based circular dependencies using a three‑level cache (singletonObjects, earlySingletonObjects, singletonFactories).

Constructor‑based circular dependencies are not solvable and result in a BeanCurrentlyInCreationException, whereas singleton beans can break setter cycles by exposing a bean early through the cache.

Common reasons for @Transactional failure include using MyISAM tables, duplicate component‑scan, placing the annotation on non‑public methods, proxy mode mismatches, and internal method calls that bypass the proxy.

Annotation‑based configuration is enabled with <context:annotation-config/> and supports @Autowired, @Qualifier, @Required, @Resource, @PostConstruct, @PreDestroy, etc.

The Spring MVC request processing flow is: DispatcherServlet → HandlerMapping → HandlerAdapter → Controller → ModelAndView → ViewResolver → View → response.

Spring MVC advantages are view‑technology independence, tight integration with the Spring IoC container, clear separation of dispatcher, handler mapping, adapter and view resolver, and flexible URL mapping.

Spring provides several advice types (before, after, around, throws) for AOP, and uses ThreadLocal to store per‑thread state for beans such as RequestContextHolder, ensuring thread safety for otherwise stateful components.

/** * 通过反射获取私有的成员变量. */ private Object getPrivateValue(Person person, String fieldName) { try { Field field = person.getClass().getDeclaredField(fieldName); field.setAccessible(true); return field.get(person); } catch(Exception e) { e.printStackTrace(); } return null; }
protected Object getSingleton(String beanName, boolean allowEarlyReference) { Object singletonObject = this.singletonObjects.get(beanName); if(singletonObject == null && isSingletonCurrentlyInCreation(beanName)) { synchronized(this.singletonObjects) { singletonObject = this.earlySingletonObjects.get(beanName); if(singletonObject == null && allowEarlyReference) { ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName); if(singletonFactory != null) { singletonObject = singletonFactory.getObject(); this.earlySingletonObjects.put(beanName, singletonObject); this.singletonFactories.remove(beanName); } } } } return (singletonObject != NULL_OBJECT ? singletonObject : null); }
protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) { Assert.notNull(singletonFactory, "Singleton factory must not be null"); synchronized(this.singletonObjects) { if(!this.singletonObjects.containsKey(beanName)) { this.singletonFactories.put(beanName, singletonFactory); this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } } }
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

aopbean-lifecycledependency-injection
IT Xianyu
Written by

IT Xianyu

We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.