skip to content

Core Container & IoC

The container itself: how beans are defined, discovered, injected, scoped and destroyed, plus the Environment, SpEL and event support everything else in Spring is built on. Interviewers start here because the rest of the portfolio only makes sense once you can explain the container.

part ofSpring Frameworkoverview, primer and where to startread it →
on this pageshow

explore

questions

224 · 7 sections

What is a BeanDefinition in Spring, and what information does it hold?

level: juniorimportance: must knowfreq 70%
basics
~20 s

A BeanDefinition is Spring's recipe for a bean: it stores metadata like the class name, scope, whether it's lazy, its constructor arguments and property values, and dependencies — the container reads it to create the actual bean instance.

open as a page

What is the difference between BeanFactory and ApplicationContext in Spring?

level: juniorimportance: must knowfreq 85%
basics
~20 s

Both are Spring IoC containers that create and wire beans. ApplicationContext is a superset of BeanFactory: it adds features like event publishing, internationalization, easy resource loading, and automatic BeanPostProcessor handling, and it eagerly creates singletons at startup.

open as a page

What is an ApplicationContext, and what are the main concrete implementations you would instantiate?

level: juniorimportance: must knowfreq 70%
basics
~10 s

ApplicationContext is Spring's IoC container that creates, wires, and manages beans. Common implementations: AnnotationConfigApplicationContext (Java/annotation config), ClassPathXmlApplicationContext (XML config), and GenericApplicationContext (flexible, programmatic registration).

open as a page

What is a Spring FactoryBean and how does it differ from an ordinary bean?

level: juniorimportance: must knowfreq 55%
basics
~20 s

A FactoryBean is a bean whose job is to build another object. When you look it up or inject it by name, Spring hands you what its getObject() method returns, not the FactoryBean instance itself.

open as a page

What is a parent-child (hierarchical) ApplicationContext in Spring, and which direction does bean visibility flow between the two levels?

level: juniorimportance: must knowfreq 55%
basics
~20 s

A child ApplicationContext can be linked to a parent. Beans defined in the parent are visible to the child, but beans defined in the child are NOT visible to the parent. Visibility flows one way: parent-to-child.

open as a page

What does @ComponentScan do, and how do you tell it which packages to scan?

level: juniorimportance: must knowfreq 80%
basics
~10 s

@ComponentScan tells Spring to search packages for classes annotated with @Component (and @Service, @Repository, @Controller) and register them as beans. You point it at packages with basePackages, e.g. @ComponentScan(basePackages = "com.app").

open as a page

What is @Conditional and the Condition interface in Spring, and how do you use them to register a bean only under certain circumstances?

level: juniorimportance: must knowfreq 62%
basics
~10 s

@Conditional is an annotation you put on a @Bean or @Configuration. It points to a class implementing the Condition interface, whose matches() method returns true/false. Spring registers the bean only when matches() returns true.

open as a page

What are @Configuration and @Bean, and how do you use them to define a Spring bean?

level: juniorimportance: must knowfreq 82%
basics
~10 s

@Configuration marks a class as a source of bean definitions. Inside it, a method annotated with @Bean returns an object that Spring registers as a bean in the application context, managing its lifecycle.

open as a page

What is functional (programmatic) bean registration in Spring, and how does it differ from declaring beans with @Bean or @Component?

level: juniorimportance: must knowfreq 55%
basics
~10 s

It registers beans by calling code — e.g. context.registerBean(MyService.class, MyService::new) — instead of using @Component scanning or @Bean methods. You give Spring a name, a type, and a lambda that creates the instance.

open as a page

What is Spring's @Import annotation and what kinds of classes can you import with it?

level: juniorimportance: must knowfreq 55%
basics
~10 s

@Import lets one Java @Configuration class pull in beans defined in another. You can import other @Configuration classes, ImportSelector or ImportBeanDefinitionRegistrar implementations, and (since Spring 4.2) plain component classes.

open as a page

What does @Autowired do, and what does it mean that Spring resolves it "by type"?

level: juniorimportance: must knowfreq 90%
basics
~10 s

@Autowired tells Spring to inject a dependency automatically. "By type" means Spring looks in its container for a bean whose type matches the field, constructor parameter, or setter parameter, and wires that bean in.

open as a page

What problem does method injection (@Lookup / lookup-method) solve when a singleton bean depends on a prototype bean?

level: juniorimportance: must knowfreq 62%
basics
~10 s

A singleton is created once, so a prototype injected normally is captured only once and reused forever. Method injection lets the singleton ask the container for a fresh prototype on every call.

open as a page

How do you inject a dependency into a Spring bean that may not exist in the context, without the application failing to start?

level: juniorimportance: must knowfreq 62%
basics
~10 s

Make the dependency optional. Use Optional<T>, or @Autowired(required=false), or ObjectProvider<T>. If no bean exists Spring injects an empty Optional / null / an empty provider instead of throwing NoSuchBeanDefinitionException.

open as a page

What does @Primary do, and when would you reach for it?

level: juniorimportance: must knowfreq 78%
basics
~20 s

@Primary marks one bean as the default choice when several beans of the same type exist. If an injection point doesn't say which one it wants, Spring picks the @Primary one instead of throwing an error.

open as a page

What is the difference in default injection semantics between @Resource and @Autowired?

level: juniorimportance: must knowfreq 75%
basics
~10 s

@Autowired matches a bean by its type first. @Resource (from jakarta.annotation) matches by name first, defaulting to the field or setter property name.

open as a page

What is a BeanFactoryPostProcessor, and at what point in the container lifecycle does it run?

level: juniorimportance: must knowfreq 55%
basics
~10 s

A BeanFactoryPostProcessor is a Spring hook that runs after the container reads all bean definitions but before it creates any beans. It can change the definitions (metadata), for example resolving ${...} placeholders.

open as a page

What is a BeanPostProcessor in Spring and when do its methods run?

level: juniorimportance: must knowfreq 72%
basics
~10 s

A BeanPostProcessor is a Spring extension point with two callbacks that run on every bean after it is constructed and dependencies are injected — one just before the bean's init method, one just after.

open as a page

What is the difference between the default singleton scope and prototype scope in Spring?

level: juniorimportance: must knowfreq 85%
basics
~10 s

A singleton bean is created once per Spring container and shared everywhere. A prototype bean gives a brand-new instance every time it is requested or injected. Singleton is the default.

open as a page

What is a circular dependency between Spring beans, and how does Spring's handling differ between setter/field injection and constructor injection?

level: juniorimportance: must knowfreq 70%
basics
~20 s

A circular dependency is when bean A needs B and B needs A. Spring can resolve this for setter/field injection (it injects a half-built A into B), but constructor injection fails because neither bean can be built first.

open as a page

What is the @DependsOn annotation in Spring and what problem does it solve?

level: juniorimportance: must knowfreq 55%
basics
~10 s

@DependsOn forces named beans to be created (fully initialized) before the annotated bean. It expresses an initialization-order dependency between beans that have no direct injection relationship.

open as a page

What is a Spring profile, and how do you activate one?

level: juniorimportance: must knowfreq 78%
basics
~10 s

A profile is a named group of beans/config that is only registered when active. Mark beans with @Profile("dev"). Activate by setting the property spring.profiles.active=dev (property file, env var, or JVM flag).

open as a page

What is Spring's Environment / PropertySources abstraction, and what does @PropertySource do?

level: juniorimportance: must knowfreq 58%
basics
~20 s

The Environment holds an ordered list of PropertySources (files, JVM system properties, OS env vars). getProperty() searches them in order. @PropertySource loads a .properties file and adds it as one more source so its keys become available.

open as a page

What is the Spring Expression Language (SpEL) and where do you commonly use it in a Spring application?

level: juniorimportance: must knowfreq 70%
basics
~10 s

SpEL is a small expression language that Spring evaluates at runtime. You write expressions inside #{...} — often in @Value — to compute values, call methods, read bean properties, or reference other beans.

open as a page

How does @Value with a ${...} placeholder work, and how do you supply a default when the property is missing?

level: juniorimportance: must knowfreq 85%
basics
~10 s

@Value("${my.prop}") injects the value of property my.prop from the environment into a field or parameter. Use ${my.prop:fallback} to supply a default when the property is not set.

open as a page

What is Spring's Environment abstraction, and how does it relate to profiles and properties?

level: middleimportance: must knowfreq 62%
basics
~10 s

Environment is a container the context exposes that models two things: which profiles are active, and a hierarchy of property sources. You inject it to read properties (getProperty) or check profiles (getActiveProfiles / acceptsProfiles).

open as a page

How do you publish and receive an application event in Spring?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Inject ApplicationEventPublisher and call publishEvent(event). Receive it with a method annotated @EventListener whose single parameter matches the event type. Spring calls that method when a matching event is published.

open as a page

What is Bean Validation (JSR-380) and how does Spring expose a Jakarta Validator you can inject?

level: juniorimportance: must knowfreq 60%
basics
~10 s

Bean Validation is a standard for declaring rules on fields with annotations like @NotNull and @Size. Spring's LocalValidatorFactoryBean builds a Validator you can inject and call validate() on.

open as a page

What is Spring's ConversionService, and how does it relate to converting a String from @Value into a typed field like an int or Duration?

level: juniorimportance: must knowfreq 55%
basics
~10 s

ConversionService is Spring's central component for converting one type to another (e.g. String to int). When @Value injects a text value, Spring uses it to coerce the String into the field's declared type.

open as a page

What is Spring's DataBinder and what problem does it solve?

level: juniorimportance: must knowfreq 55%
basics
~10 s

DataBinder takes a bag of name/value pairs (like form fields) and sets them onto a target object's matching properties. It collects any problems in a BindingResult instead of throwing.

open as a page

What is Spring's MessageSource, and how do you use getMessage to resolve a localized message with arguments?

level: juniorimportance: must knowfreq 55%
basics
~20 s

MessageSource is Spring's interface for resolving text by a code for a given Locale. You call getMessage(code, args, locale); args fill placeholders like {0} in the properties file, so one code renders in different languages.

open as a page

What happens to an exception thrown inside a void @Async method, and how is that different from a normal (synchronous) method call?

level: juniorimportance: must knowfreq 70%
basics
~20 s

In a synchronous call the exception bubbles up to the caller. In a void @Async method it runs on another thread, so the caller never sees it — Spring catches it and, by default, just logs it.

open as a page

What does @Async do in Spring, and what must you do to switch it on?

level: juniorimportance: must knowfreq 78%
basics
~10 s

@Async marks a method so Spring runs it on a separate thread instead of the caller's thread. It only works if you add @EnableAsync (or Boot's equivalent) to a configuration class first.

open as a page

What does @EnableCaching do, and how does @Cacheable turn a method into a cached method?

level: juniorimportance: must knowfreq 78%
basics
~20 s

@EnableCaching switches on Spring's caching support (an AOP proxy). @Cacheable on a method means: before running it, look in the named cache by key; if found, return the cached value and skip the method; if not, run it and store the result.

open as a page

What are the built-in CacheManager implementations Spring ships, and when would you use each?

level: juniorimportance: must knowfreq 55%
basics
~10 s

Spring provides ConcurrentMapCacheManager (in-memory maps, good for tests), CaffeineCacheManager (Caffeine with eviction/TTL, common in prod), NoOpCacheManager (disables caching), CompositeCacheManager (combines several), and JCacheCacheManager (JSR-107 providers).

open as a page

What is Spring's TaskDecorator, and why is it needed when running work on a thread pool like ThreadPoolTaskExecutor?

level: juniorimportance: must knowfreq 55%
basics
~20 s

TaskDecorator is an interface with one method, decorate(Runnable), that wraps every task before a ThreadPoolTaskExecutor runs it. You use it to carry thread-local context (like logging MDC or the logged-in user) from the calling thread onto the pool thread.

open as a page