120 Spring Framework Interview Questions – All in One PDF!
120 Spring Framework Interview Questions – All in One PDF!
Spring Core
1)What is Spring?
➔Spring is an open source java framework.
-Using spring we can develop Standalone&Enterprise application.
-Spring was initially release 2003.
-Spring was production release(1.0) in 2004.
-Spring was developed by “Rod Johnson”.
5)Which jar files are needed to spring configuration file xml schema?
➔i)Spring-beans-xxx.jar
ii)Spring-core-xxx.jar
iii)Spring-context-xxx.jar
Created by:-Lalit Ingale
iv)Common-logging-xxx.jar
v)Spring-expression-xxx.jar
6)Define @Component?
➔The “@Component” annotation in Spring is used to declare a class as a
spring bean, which is a managed component in the spring application
context.
-It helps spring automatically detect and manage these beans during
application startup, making them available for dependency injection and
other spring feature.
7)Define @Value?
➔The @Value annotation in spring is used to inject values in spring bean
fields or methods.
-@Value is mostly used to inject values from external sources(e.g,
properties files or environment variable)
9)Define @ComponentScan?
Created by:-Lalit Ingale
12)What is BeanPostProcessor?
Created by:-Lalit Ingale
14)Define constructor-arg?
➔ In spring framework, constructor-arg is used in XML configuration to
pass values to a constructor when spring creates an object(bean).
15)Define getBean()?
➔In Spring Framework, getBean() is a method used to get or create an
object(bean) from the Spring Container.
17)Define @Configuration?
➔@Configuration in Spring marks a class as providing bean definition for
the application context, enabling java based configuration.
18)Define @Bean?
➔@Bean is Spring marks a method to produce a bean object which is
managed by the spring container.
Created by:-Lalit Ingale
19)Define AutoWiring?
➔It is the feature of Spring framework by which we can achieve “DI
automatically”.
- Auto Wiring
20)Define @Autowired?
➔-The @Autowired annotation in spring is used for automatic
dependency injection.
-It tells the Spring: “Hey, just grab the right piece(bean) and plug it in here
to make this work.
-In short, it simplifies your code by letting spring handle object connection
for you.
21)Define @Qualifier?
➔The @Qualifier annotation in Spring helps pick the right bean among
multiple beans of the same type, It helps spring to know which bean you
want injected, resolving ambiguity.
24)Explain autowire-candidate?
➔In Spring, autowire-candidate means:”Can this bean be chosen for
autowiring or not?”
-Think of it like a yes/no flag you set on.
-“autowire-candidate=false”means:
Don’t use this bean when spring is trying to autowire dependencies.
-“autowire-candidate=true”(default value) means:
25)What is MAVEN?
➔-It is a build tool which automates everything related to the building of
project.
-Maven was developed by JASON VAN ZYL in 2004(Apache software
foundation)
34)Define @RequestParam?
➔ @RequestParam is used in Spring MVC to get values from the URL
query string or form data and pass them into your controller method.
35)Define Model?
➔ Model is like a container that holds your data so that it can be shown in
the View (like an HTML page).
36)Define @ModelAttribute?
➔This annotation are used to take the data for form, and store in
object(model) Automatically.
➔The java class whose objects is created and managed by IOC container is
called Spring Bean.
42)What is Configuration?
➔The process of giving inputs and instruction to IOC container is called
configuration.
43)Which are the 3 approaches are used to give inputs and instruction to
the spring programming?
➔a)using xml driven cfgs(all inputs and instruction will be given to IOC
container using xml file)
b)using xml+annotation driven cfgs(first the inputs and instruction will
be given using annotation,if not sufficient then we go for xml file)
c)using 100% java code configuration(All inputs and instruction will be
given using both annotation and java statement i.e xml file will be
eliminated or minimized).
52)Using FileSystemXmlApplicationContext.
➔Creates the ApplicationContext IOC container by locating the given spring
bean cfg file from the specified file system.
➔FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext(“name and
location of spring bean cfg file”);
Created by:-Lalit Ingale
53)Using ClassPathXmlApplicationContext.
➔Creates the ApplicationContext IOC container by locating the given spring
bean cfg file from the directories and jar files added to the CLASSPATH.
➔ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(“name of the
spring bean cfg file that will be located from the directories and jar files added to the classpath”)
54)Using XmlWebApplicationContext.
➔Useful to create IOC container in web application by taking <servlet
logical name>servlet.xml file as the spring bean cfg file from the WEB-INF
folder of the web application.
➔XmlApplicationContext ctx = new XmlApplicationContext();
(if the servlet comp logical name is “controller”
then it takes WEB-INF/controller-servlet.xml file
as the spring bean cfg file)
55)Using AnnotationConfigApplicationContext.class .
➔Useful to create ApplicationContext IOC container in standalone apps by
taking given java class as the configuration class as alternate to xml file.
➔AnnotationConfigApplicationContext ctx = new annotationConfigApplicationContext(AppConfig.class);
@Configuration Class
56)Using AnnotationConfigWebApplicationContext.
➔This is very useful to create IOC container in web application setup by
taking given java class as the @Configuration class.
➔AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(AppConfig.class);
Created by:-Lalit Ingale
Dependency Dependency
Lookup(DL) Injection(DI)
================== ====================
i)For this we need to call This can be implemented in multiple ways by using
getBean(-) on IOC contain- xml driven cfgs or annotation driven cfgs.
er obj passing the bean id ➔As setter injection
➔As constructor injection Basic Injection
➔As arbitrary method injection
➔As filed injection
➔Interface Injection
➔Lookup Method Injection Advanced Injection
➔Method Injection
➔If the IOC container using setter method of target spring bean class to
assign the dependent spring bean class obj then it is called setter injection.
66)Which tags are used to inject that property in xml driven cfgs?
➔In xml driven cfgs we use:
a)<property> tags under <bean> tag for setter injection cfgs.
b)<contructor-arg> tags under <bean> tag for constrctor injection cfgs.
67) Which tags are used to inject that property in Annotation or java code
driven cfgs?
Created by:-Lalit Ingale
68)What is XML?
➔XML(Extensible Markup Language) is a flexible, structured format used
to store and transport data, It is both human-readable and machine-
readable.
-first create TSBCO and then DSBCLO. spring bean class obj.
ii)For this, use <property> tag in xml driven cfgs. ii)For this use <constructor-arg> tag in xml
driven cfgs.
iii)For this, use @Autowired on the top of the iii)For this, use @Autowired on the top of
setter method in annotation driven,java code param-constructor in the annotation driven
driven cfgs. cfgs.
iv)Supports Cyclic or Circular DI. iv)The IOC creates dependent spring bean
v)For this injection IOC container uses 0-param- class obj first then it creates target spring
constructor to create target spring bean class target spring bean class obj having depen-
object. dent spring bean class obj as the constructor
vi)Bit slower injection compare to constructor arg value.
injection. v)Does not support Cyclic or Circulare DI.
vii)Good to use if the property of spring bean constructor to create target spring bean class
➔FileSystemXmlApplicationContext:-
-This class creates the IOC container of type ApplicationContext by locating
the given spring bean file from the specified path of file system, So we can
pass either relative path(respect to project location) or absoulate path(right
from root folder) of spring bean cfg file.
➔ClassPathXmlApplicationContext:-
-This class creates ApplicationContext IOC container by locating the given
spring bean cfg file from the folders and jar files added to the
CLASSPATH/BUILDPATH of the project.
-By default “src” folder of the Eclipse project is placed in the CLASSPATH or
BUILDPATH of project automatically.
83)Define @Lazy?
➔@Lazy means "Don't create this object now — wait until it's really
needed."
84)Define @Scope?
➔-@Scope tells Spring how long an object (bean) should live and how it
should be shared.
Created by:-Lalit Ingale
-It decides whether to create one object for the whole app, or a new object
every time someone asks for it.
89)What are two special properties that the singleton scope spring bean
is having compare to other scope spring beans?
Created by:-Lalit Ingale
➔ A singleton scope Spring bean has only one instance for the entire
application and is created at startup, unlike other scopes where a new
instance is created when needed.
90)How can we get singleton class effect on spring bean without making
the spring bean class as singleton class?
➔ @Scope("singleton") - Spring ensures only one bean instance is created
and shared, even if the class allows multiple objects.
93)If we do not provide bean id to the spring bean class then what
happens?
➔The IOC container generates default bean ids to the spring bean class
based on the approach that we have used to configure the spring bean class.
Created by:-Lalit Ingale
95)When should i take the spring bean scope as the prototype scope?
➔If the data of the spring bean is changing time to time (like storing from
data to the spring bean class obj) then we need to take that spring bean as
prototype scope spring bean.
96)Define @Scope(“request”)?
➔If want to store the form data/front end UI of a web application or
distributed app in the spring bean class obj which changes request to
request then prefer keeping spring bean class obj in the request scope.
97)Define @Scope(“session”)?
➔Keeps the spring bean class obj ref in the session obj as the session
attribute value i.e spring bean class object is specified to one browser s/w
of client machine.
98)Define @Scope(“application”)?
➔If u want to store request count, users count of a web application in
spring bean class obj then better to put spring bean class obj in application
scope.
➔-Singleton scope means 1 obj for spring bean per bean id with respect to
each IOC container.
-application scope means 1 obj for spring bean per bean id with respect to
entire web application.
100)Define @Scope(“websocket”)?
➔In spring programming, when @Scope(”websocket”) makes the IOC
container to keep the spring bean class obj in the websocket.session that
supports full duplex communication.
107)How to configure multiple spring bean cfg files in our spring apps?
➔@PropertySource({“com/nt/commons/Info1.properties”,”com/nt/comm
ons/Info.properties”})
108)If multiple properties files that are configured with spring app are
having same keys and with different values then the @Value annotation
having that key injects which value as the final value?
➔The lastly configured properties file (in @PropertySource Anntation) key
maintained value will be picked up for injecting the value to the spring bean
Created by:-Lalit Ingale
property.
109)What is I18n?
➔The process of making our app working for different Locales is called
I18n.
110)Define ctx.getMessage()?
➔Is given to get message from the properties file based on the given key
and given Locale object information.
112)How to change the dependent spring bean of target spring bean when
multiple possible dependent spring beans are there?
➔Can be done in two ways:-
a)using spring bean cfg file + alias name for bean id + properties file +
@Qualifier().—not a great solution becoz we are entertaining the spring
bean cfg file(xml file)
b)using profile concept—Best solution to solve the ambiguity problem with
loose coupling.
Created by:-Lalit Ingale
114)Where should i use BeanFactory IOC container and where should I use
ApplicationContext container?
➔If spring is used in Memory Sensitive apps like mobile apps, embedded
system apps, and IOT apps….where 1 or 2 few extra kilo bytes also matters
then prefer using BeanFactory IOC container.
➔If spring is used in web application, distributed Apps, enterprize Apps,
and etc…Where Memory utilization does not matter then prefer using
ApplicationContext container.
115)For IOC container of 100% Code driven cfgs, if we can not give certain
inputs or instruction through java code and annotation then how to pass
such special instruction to IOC container?
➔ We can give these special instruction to IOC container through xml file
by linking the file with @Configuration class using @importResource
annotation.
116)Define @importResource?
➔ The @ImportResource annotation in Spring is used to import XML-based
Spring configuration files into a Java-based configuration class.
Created by:-Lalit Ingale
THANK
YOU