Spring Application Context

  • Spring ApplicationContext is where Spring holds instances of objects that it has identified to be managed and distributed automatically.
  • The objects are called Beans.
  • Spring collects bean instances from our application and uses them at the appropriate time.
  • All threads in spring boot share same application context and beans

Stereotype annotations

  • Stereotype annotations are specialized annotations that are used to indicate the role or purpose of a particular component
  • examples:
    • @Component (main stereotype annotation): generic stereotype for any Spring-managed component
      • @Service: indicate service layer + @Component
      • @Controller: indicate presentation layer + @Component
      • @Repository: indicate persistence layer + @Component + it is supported as a marker for automatic exception translation
      • @Configuration: indicate the class has @Bean annotated functions + @Component + CGLIB intercepting etc.

@Component vs @Service

  • They are both same except they indicate the kind of class it is.

@SpringBootApplication

  • @SpringBootApplication is composed of:
    • @Configuration
    • @EnableAutoConfiguration
    • @ComponentScan

@ComponentScan

  • @ComponentScan annotation performs scanning the packages and creating Beans and gathering into Application Context.
  • Component-scanning is done starting in the same package as the @SpringBootApplication annotated class is in. So @SpringBootApplication should be at the root level.

@Bean

  • It is used on methods and not at class level
  • we annotate methods with @Bean so that Spring can store the method’s result as a Spring bean.
  • The methods which are annotated with this annotation, have the corresponding class annotated as @Configuration
  • Name of the bean will be same as the function name

@Component vs @Bean

  • @Component is a class-level annotation, but @Bean is at the method level, so @Component is only an option when a class’s source code is editable. @Bean can always be used, but it’s more verbose.
  • @Component is compatible with Spring’s auto-detection, but @Bean requires manual class instantiation.
  • Using @Bean, decouples the instantiation of the bean from its class definition. This is why we can use it to make third-party classes into Spring beans. It also means we can introduce logic to decide which of several possible instance options for a bean to use.
  • https://stackoverflow.com/questions/10604298/spring-component-versus-bean

@Configuration

  • This class level annotation indicates that the class will be used by JavaConfig as a source of bean definitions
  • You should use @Bean methods inside @Configuration annotated class. It enables interception of beans via CGLIB (which can cache and return the bean even if function is directly called)
  • If @Configuration is not used, @Bean methods run in lite mode without interception with less functionality

Bean Naming

  • Every bean is identified by its name
  • Class Level
    • uses the class name and converts the first letter to lowercase
    • example: @Component, @Controller, @Service etc.
    • custom naming: @Component("myBean")
// will create bean with name `loggingService`
@Service
public class LoggingService {
}
  • Method Level
    • using @Bean, method name is the name of the Bean.
    • custom naming: @Bean("myBean")
  • If the name clashes then, based on the configuration, beans are either overridden or throw exception
  • Custom naming using @Qualifier
    • can be used on both class and method level
@Service
@Qualifier("logger")
public class LoggingService {
}

Spring Bean Scopes

BeanFactory vs ApplicationContext

  • ApplicationContext is an interface

@Inject vs @Autowired

Spring Beans lifecycle???