Table of contents
No headings in the article.
Hey Reader !!!
I hope you are doing good and also I hope you are preparing well for the your next up-coming interviews for Backend Role.
I assume you have already read the Interview questions on Spring Boot - 1, in this article let's explore some of the important questions which can be expected in a Backend role using Spring Boot as the tech stack.
- What is the difference between a Monolithic and Micro-service architecture?
In monolithic architecture, applications are built as one large system, whereas in micro-service architecture, applications are divided into modular components which are independent of each other.
In monolithic architecture, if any fault occurs, the entire application might bring down as everything is tightly coupled. But in case of Micro-service, if the fault occurs it affects only that particular micro-service and its consumers.
Each micro-services can be scaled independently based on the requirement. Example: if any one of the micro-service is taking more traffic then you can deploy another instance of that micro-service so we can distribute the traffic. But in case of monolith, even if we want to scale one service within the monolith application, we will have to scale the entire monolith application.
In case of monolith, the entire technology stack is fixed at the beginning of the project. And it will be difficult to change the technology at the later stage. But in Micro-service, as the services are independent of each other they can be coded in any language. So micro-service gives you the freedom to choose difficult technologies and frameworks.
Deploying a new version of a service in monolith requires more time and it increases the application downtime. However, in micro-service it is comparatively takes lesser downtime.
What is Dependency Injection in Spring ?
Dependency Injection is the most important feature of Spring framework.
Dependency Injection is a Design Pattern where dependencies of a class are injected from outside like from .xml files, it ensures loose-coupling between classes.
In a spring MVC applications, the controller class has the dependency of service layer classes and service layer have dependencies of DAO layer classes.
What are the Different Types of Dependency Injection?
Spring provides two ways to inject dependencies
By Constructor
By Setter Method
Constructor Based DI
When the required dependencies are provided as arguments to the constructor, then it is known as Constructor-based dependency injection
Injecting a dependency is done through bean-configuration file, for this we use <constructor-arg> xml tag
<bean id = "classA" class = "com.dependency.A"/> <bean id = "classB" class = "com.dependency.B"/> <constructor-arg ref = "classA"/> </bean>
- Incase of more than one dependency, the order sequence of constructor arguments should be followed to inject the dependencies.
Using Java Based Configurations:
- When using java based configurations, the constructor needs to be annotated with @Autowired annotation to inject the dependencies.
Setter-Method Injection
In this, the required dependencies are provided as the field parameters to the class and the values are set using setter methods of those properties.
<bean id = "classA" class = "com.dependency.A"/> <bean id = "classB" class = "com.dependency.B"/> <property name = "a"> <ref bean = "classA"> </property> </bean>
What is @Autowired Annotation?
- @Autowired annotation is a way of telling spring that auto-wiring is required and it can be applied to fields, constructors and methods.
What is the difference between BeanFactory and ApplicationContext?
BeanFactory is the most basic version of IOC containers which should be preferred when memory consumption is critical, whereas ApplicationContext extends BeanFactory, so we get all benefits of BeanFactory plus advanced features of enterprise applications.
BeanFactory instantiates beans on-demand i.e. when the method getBean(beanName) is called, it is also called Lazy initializer whereas ApplicationContext instantiates beans at the time of creating the container where bean scope is Singleton, so it is an Eager initializer.
BeanFactory only supports 2 bean scopes, singleton and prototype whereas ApplicationContext supports all bean scopes.
Annotation based dependency injection is not supported by BeanFactory whereas ApplicationContext supports it.
ApplicationContext provides additional features like MessageSource access (i18n or Internationalization) and Event Publication.
What are the different scopes of a Bean?
Spring framework supports 5 scopes,
singleton – only one bean instance per Spring IOC container
prototype – it produces a new instance each and every me a bean is requested.
request – a single instance will be created and made available during complete life-cycle of an HTTP request
session – a single instance will be created and made available during complete life-cycle of an HTTP session
global session – a single instance will be created during the life-cycle of a ServletContext.
@Scope annotation or scope attribute of bean tag can be used to define bean scopes in Spring
How to inject a prototype scope bean in a singleton scope bean?
When a prototyped scoped bean is injected in a singleton scoped bean, then on each request of singleton bean, we will get the same instance of prototype scoped bean, but there are certain ways where we can get a new instance of prototyped scoped bean also
By injecting an ApplicationContext in Singleton bean and then getting the new instance of prototyped scoped bean from this ApplicationContext
Lookup method injection using @Lookup
Using scoped proxy
To inject the ApplicationContext in singleton bean, we can either use @Autowired annotation or we can implement ApplicationContextAware interface
Using @LookUp method Injection
Here, Spring will dynamically overrides getPrototypeBean() method annotated with @Lookup and it will look up the bean which is the return type of this method. Spring uses CGLIB library to do this.
Using Scoped Proxy
What is auto-configuration in SpringBoot?
Spring applications have a lot of XML or Java Bean Configurations.
Spring Boot Auto configuration looks at the jars available on the CLASSPATH and configuration provided by us in the application.properties file and it tries to auto-configure those classes
What is @Bean annotation?
@Bean annotation is used when you want to explicitly declare and register a bean into application context, so that it will be managed by Spring.
When using @Bean, you have the control over the bean creation logic.
@Bean is a method level annotation, the body of the method contains the logic for creating the bean instance and this method returns the instance which will be registered in the spring application context.
Using @Bean, you can register the classes from 3rd party libraries into the application context
@Bean annotation is usually declared in configuration classes
I hope this article is helpful. Thank you for reading the article. Please like, share and comment. it will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!!!