🌱 Spring Core — interview questions

11 questions. Filter by what the role asks for; open one for the six layers of the answer. The course is where the answers come from.

11 of 11

01A teammate says "we use Spring, so of course this class does dependency injection" about a class that calls `applicationContext.getBean(PricingClient.class)` inside a method. Are they right?Dependency injectioneasy0-2y02A `@Component` reads an `@Autowired` field inside its own constructor and gets `null`, even though the same field is populated correctly in every other method. What's actually happening, and why does constructor injection not have this problem?Dependency injectioneasy0-2y03Two beans, `A` and `B`, each need the other. With constructor injection, Spring fails at startup and says so. With field injection, the exact same cycle can start successfully. What's the actual mechanism behind that difference, and is the field-injection version actually fine?Circular dependenciesmedium3-5y04A bean implements both `InitializingBean` and has a `@PostConstruct` method. Which callback runs first, and why does that order surprise almost everyone who guesses?Bean lifecyclemedium3-5y05A `@Bean @Scope("prototype")` class implements `AutoCloseable`, and its `close()` method never runs even though a `destroyMethod` is configured. Separately, a singleton needs to hold a reference to a request-scoped bean. What's the shared mechanism behind why the first one fails silently, and what makes the second one possible at all?Scopesmedium3-5y06A production service's `@Transactional` method silently isn't transactional — no error, no exception, it just doesn't roll back on failure. The startup log has one `INFO` line: "Bean 'myService' of type [MyService] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)". What does that warning actually mean, and why does it happen?Bean lifecyclehard5-8y07A code-cleanup refactor makes a repository class's methods `final` "since nothing should override them." Four months later, a database failover reveals that `@Retryable` on one of those methods had been doing nothing since the refactor. Why did marking a method `final` silently disable the annotation, with no error at any point?AOP proxieshard5-8y08A production incident is fixed by editing `application-prod.yml` to raise a timeout from 500ms to 3000ms, and the deploy confirms the value took effect. Six weeks later the timeouts return, and the file still says 3000. What actually happened, and how would you have found it faster?Configurationmedium5-8y09Two of your own `@Configuration` classes, in different modules, each declare `@Bean @ConditionalOnMissingBean PricingClient pricingClient()` as a defensive default, expecting exactly one to win. In practice, which one wins is inconsistent between builds. Why is relying on `@ConditionalOnMissingBean` between two of your own configuration classes fundamentally different from relying on it against Spring Boot's auto-configuration, and what should you do instead?Configurationhard8+y10A `@Configuration` class has one `@Bean` method call another `@Bean` method directly — `return new OrderService(pricingClient())` — expecting it to behave like an ordinary Java method call. Twice in the same context, that call returns the same singleton object both times. Why, mechanically, does that happen, and what changes if the class is annotated `@Configuration(proxyBeanMethods = false)`?Configurationhard8+y11A method is annotated with both `@Transactional` and `@Cacheable`. A call that fails partway through, after the value was already computed and cached, rolls back its database writes — but the next call still returns the cached (now-inconsistent) value instead of recomputing it. What determines which annotation's advice actually wraps which, and how would you fix the ordering?AOP proxiessenior8+y