Spring Core

IoC Container

The IoC container manages Dependency Injection process and subsequent bean management. Interface BeanFactory provides an advanced configuration.

ApplicationContext is derived from BeanFactory and adds extra functionality:

  • Easier integration with Spring AOP
  • Message Resource handling
  • Event publication
  • Application layer specific context, such as WebApplicationContext.

BeanFactory provides configuration framework and configuration while ApplicationContext add more enterprise-specific functionality.

Spring separates application configuration from application objects (beans) and manages these objects by creating them in a correct dependency order and ensuring that they’re fully initialized.


  • Singleton (prototype)
  • Prototype – created once per invocation
  • Request (Http request)
  • Session (Session request)
  • Application (Servlet context)
  • Web Socker (Web socket)

Prototype should be used for stateful beans and Singleton for stateless beans.

For Prototype, destruction lifecycle callback are not called, prototype scope object must be cleansed manually.

Request, Session and Application are only available for Web Aware ApplicationContext implementations.

@Bean vs @Component

@Bean is used when auto-detection is not possible, like to integrate existing libraries while @Component are automatically detected by Spring.

@Bean are configured in a @Configuration class at method level, it decouples instanteation from definition.

@Component work at class level and can be only if the class is editable.


@Components – is a generic stereotype for any Spring managed components.


@Service – annotates classes at the service layer, code that holds business layer.

@Repository – annotation used for classes from the persistence layer. Catches specific exceptions and re-throws them as Spring unified unchecked exceptions.

@Controller (@RestController) – denotes a frontend endpoint.

Spring uses @ComponentScan to gather @Component classes and it’s derivates to Application Context.

Application Context

Application Context interface represents the IoC container and is responsible for instantiating, configuring and assemble the beans. The container gets the information how to configure, assemble and instantiate the beans through metadata.



There are three ways to configure Application Context:

XML , the old and first way to configure beans and resources using xml files.

Java based, it is a replacement to XML configuration, defines external beans to the application classes. It also uses annotations but in terms of @Bean, @Configuration, @Autowired, @Import and it’s still used for integrations with external code.

Annotations, also using Java but using autoscan and a different set of annotations: @Component, @Service, @Repository, ..

Spring application context can be created in any environment: standalone applications, web application or Junit tests.