Skip to content

Instantly share code, notes, and snippets.

@anandchakru
Last active December 2, 2021 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anandchakru/934d570ad8caef01033565c785829219 to your computer and use it in GitHub Desktop.
Save anandchakru/934d570ad8caef01033565c785829219 to your computer and use it in GitHub Desktop.

Spring boot questions

Spring dependency injection

Fundamental concept in spring framework. Spring injects dependencies into other objects enabling loose coupling of components and easy to manage/change/test dependencies.

Spring Inversion of control/Dependency injection

It is a process by which objects define their dependencies through external ways such as constructors, factory methods, properties, annotations.

What these interface used for?

InitializingBean - enforces the implementing class to implement afterPropertiesSet, a method called during spring bean initialization.

DisposableBean - enforces the implementing class to implement destroy, a method called during spring container destruction.

BeanPostProcessor - enforces the implementing class to implement postProcessBeforeInitialization' and postProcessAfterInitialization`, methods used to customize instantiation/dependency-resolution before/after spring initialization.

FactoryBean<T> - enforces the implementing class to implement T getObject(), method to invoke while creating an object T, which the implementing class is suppose to produce in factory pattern. boolean isSingleton() can be implemented to specify if this factory returns singleton instance or not (Default true).

Spring APO, what is it?

Aspects enable the modularization of concerns that cut across multiple types and objects, eg: transaction management. They are also called as crosscutting concerns. IoC doesn't depend on AoP, so its optional to use.

Aspect modularization of a concern that cuts across multiple classes.

Join point point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, its always a method execution.

Advice action taken by an aspect at a particular join point. Types: around, before & after.

Pointcut predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut.

Introduction declaring additional methods or fields on behalf of a type. Spring AOP lets you introduce new interfaces, and a corresponding implementation to any advised object

Target object an object being advised by one or more aspects also called advised object .

Weaving linking aspects with other application types or objects to create an advised object. This can be done at compile time (using AspectJ), load time, or at runtime. Spring AOP (and other pure Java AOP frameworks) does weaving at runtime.

Spring AOP advice types

  • Before
  • After returning
  • After throwing
  • After
  • Around

What are some of spring-boot starter? How does it work?

Spring boot provides auto starters which help quickly write an application with sensible defaults. most commonly used are

  • Web starter.
  • Data JPA starter.
  • Security starter.
  • Thymeleaf starter.

Can you replace @SpringBootApplication with other annotations combined, what are they?

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes.

WHat is the difference between @ComponentScan and @Component in Spring ?

@ComponentScan scans the specified package(s) for all the spring components. @Component indicates that a class is a spring component.

List couple of Basic, commonly used annotations in Spring Boot

  • @EnableAutoConfiguration
  • @SpringBootApplication

How to disable a specific auto-configuration in spring boot?

Use excludes arg provided by @EnableAutoConfiguration

Difference between @RestController and @Controller

Generally @Controller is meant for human consumption format eg, HTML. @RestController is used for machine consumption in JSON/XML format.

What are spring profiles

Meant to be used for different environment during devlopment, testing and production phases of software developement.

Spring bean lifecycle

  • Instantiate
  • Populate properties
  • Call setBeanName (BeanNameAware)
  • Call setBeanFactory (BeanFactoryAware)
  • Call setApplicationContext (ApplicationContextAware)
  • PreInitialize (Bean Postprocessors)
  • afterPropertiesSet
  • Custom Init method
  • PostInitialize (Bean Postprocessors) ~~
  • destroy (Disposable Beans)
  • Custom Destory method

Spring aware interfaces

  • ApplicationContextAware
  • ApplicationEventPublisherAware
  • BeanClassLoaderAware
  • BeanFactoryAware
  • BeanNameAware
  • BootstrapContextAware
  • LoadTimeWeaverAware
  • MessageSourceAware
  • NotificationPublisherAware
  • PortletConfigAware
  • PortletContextAware
  • ResourceLoaderAware
  • ServletConfigAware
  • ServletContextAware full list of aware
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment