Spring Interview Questions 2021 PART — 1
What is frameworks ?
Framework is installable software that uses existing technologies internally to simplify the application development process that means it provides an abstraction layer on existing technologies to simply the application development process.
Example : Struts, Spring, Hibernate.
While working with technologies the programmer has to develop both common as well as specific logic of application.
Where as while working with framework the programmer has to develop only application specific logics because the common logic will be generated by the framework dynamically.
Note : Every Framework uses technology internally but never makes programmer to know about it.
What is Spring ?
Spring is a opensource, light weight, loosely coupled, aspect oriented, dependency injection based java application framework to develop various java applications.
Along with spring software installation we get its source code. This makes spring as open source.
Spring is light weight because it comes as zip file having less size.
What is dependency Lookup ?
If a resource/class/object is searching and getting its dependent value from other resources then it is called as dependency lookup.
For Example :
The way we get object from JNDI registry is called Dependency Lookup.
The way we get JDBC connection object from JDBC connection pool is called Dependency Lookup.
What is dependency injection ?
If underlying container/server/framework/JRE assign values to resources/class/object dynamically at run-time then it is called dependency injection.
Here the dependent values will be pushed to the resource.
JVM calling constructor automatically to assign initial values of object when object is created.
Servlet container injects ServletConfig object to our servlet class object by calling init(ServletConfig config) method when servlet container creates our Servlet class object.
FileSystemResource Vs ClassPathResource
Explain about Spring Modules.
Spring Framework consists of 20 modules which are grouped into Core Container, Data Access/Integration, Web, AOP, Instrumentation, Messaging, Test.
Core Container : The core of the Spring Framework contains 4 modules.
Spring Core, Spring Bean, SpEL (Spring Expression Language), Spring Context
Data Access/Integration : Supports database interactions with 5 modules.
JDBC, ORM, OXM, JMS, Transaction
Web : Adds support for creating a web application using 4 modules.
Web, Web-MVC, Web-Socket, Web-Portlet
What are the types of IoC container ?
There are two types of Container Spring Framework has
- Bean Factory : This is a basic container which performs spring bean management and dependency injection.
- Application Context : This is Advanced Container which internally uses BeanFactory container so that it can perform advance operations along with Bean management and dependency injection such as integration with spring AOP, message resource handling for i18n etc.
- To activate/start BeanFactory container we should create object of a class that implements “BeanFactory”.
BeanFactory factory = new XmlBeanFactory(Resource object)
Explain different types of scopes in Spring ?
We can make IOC container to keep our spring bean class objects in different scopes. These are
- Singleton : Always returns same bean class object when requested.
- prototype : It returns separate bean class object each time when requested.
- request : The bean class object will is created per HTTP request.
- session : The bean class object will be created per HTTP session.
- globalsession : It is specific to portlet environment.
Note : Important Observation
- The Bean Score “singleton” never makes our bean class as singleton java class. But gives singleton behavior while creating object.
- If Bean Scope is singleton then IOC container creates the bean class object and keeps in HashMap element as value by having bean “id” as key and uses that object across the multiple
factory.getBean(“ — ”)method calls.
- If scope is “prototype ”then IOC container doesn’t keep the created bean class object in HashMap so it returns new object for every
factory.getBean(“ — ”)method call.
- If scope is “request” then bean object will be maintained as request attribute.
- If scope is “session” then the bean object will be maintained as session attribute.
What are the common implementations of the ApplicationContext ?
The 3 most popular container are :
What is Bean Wiring and what are its types ?
Configuring beans and their dependency injection in spring bean configuration file is called wiring.
There are two types of wiring
- Explicit Wiring : For Explicit wiring we use
<constructor-arg>tag for dependency injection configuration.
- Auto Wiring : Here, IOC container automatically detects the dependency and injects them to target objects. Here, no
<property>. <constructor-arg>tag are required.
What are the Limitation of Auto Wiring ?
The limitation of Auto Wiring are -
- Can be used to inject object only but not the simple values.
- If IOC container have multiple dependencies to inject then ambiguity problem may raise.
- It kills the readability of XML file, so the bug fixing becomes very complex.
Can you elaborate Auto Wiring in Spring ?
Here, IOC container automatically detects the dependency and injects them to target objects. Here, no
<property>. <constructor-arg> tag are required.
There are 4 methods of performing Auto Wiring
- Performs setter injection.
- Here there is no possibility of getting ambiguity problem.
- Perform setter injection.
- For this target class property type and dependent class must match or must be compatible with each other.
- There is a chance of ambiguity problem.
- If we configure dependent class one more time in spring bean configuration file with different bean id then we get ambiguity problem.
- Uses parameterized constructor to perform constructor injection.
- There is a chance of getting ambiguity problem. If multiple dependencies are found, but it throws exception if target class doesn’t contain 0-param constructor.
- If target class contains 0-param constructor then it perform “byType” mode of Auto Wiring.
- If target class contains parameterized constructor then it perform “constructor” mode of Auto Wiring.
- Removed from 3.0 onwards.
Setter Injection Vs Constructor Injection
What is Spring Bean ?
- The java class whose object can be managed by spring container is called spring bean.
- Spring bean can be user-defined/pre-defined/third party class.
- We can’t take abstract class/interface as spring bean because it can’t create object for abstract class or interface.
What is inner bean ?
- It is no way related to physical level inner classes of java.
- It is all about configuring one bean as inner bean of another bean by placing
- Inner bean cannot be re-used or shared with other beans.
- Inner bean cannot be accessed from client applications.
- In practice, defining inner beans affects the readability of spring configuration xml.
What do you mean by bean inheritance ?
- It is no way related to class level physical inheritance.
- It is all about XML file level bean configuration.
What is Bean Alias ?
- It is all about providing more alternative names/id’s to use bean.
- Before Spring 2.0 “name” attribute was given to provide “alias name”.
- From Spring 2.0 <alias> tag is introduced for the same.
- We can use both attribute, <alias> tags to provide alias names from Spring 2.0.
- <alias> tag is recommended to use because it allows to provide alias names to multiple bean in single place.
- Aliasing is useful in the maintenance mode of bean configuration where we can short alias name for lengthy bean ids.