Top 10 spring interview questions and their answers. Spring basic concepts.

Spring framework is the most recognized framework in java right now . So if you are thinking about changing job in java profile spring framework knowledge is must . Spring IOC and spring-mvc are two hot topics of any interviewer in java these days .The basic categorisation any interviewer will do in front of any interviewee will be :

  1. spring IOC
  2. Dependency Injection
  3. spring security
  4. spring mvc
  5. spring AOP

 

some less recognized topics also like spring jdbc , spring  JMS but they can be only ask by person who seriously worked on them.  Let’s start with spring IOC and dependency injection first.

Top10  spring interview questions and their answers

Question 1 : What is Dependency Injection

Answer :  Mostly every interviewer start interview this question of spring if he requires spring knowledge in a candidate. As it is explained by name when you injecting dependency of one  java object into another java object that is called dependency injection. Then interviewer  will ask Do I need spring framework for dependency injection then answer is  No.  We were doing it since java started with association of one java object into another that is also dependency injection . Then why do, I need spring for dependency injection. We do not need spring framework for dependency injection we need it for Inversion of control . Means without spring we need to initialize our associated class(injected) class with new   keyword of java but with spring framework is doing this for us. Now he will think that you know basic fundamental of spring at least.

Question 2 : What is Inversion of control as you mentioned in your answer and how spring is defining object for us without using new keyword.

Answer :  Now you will say that spring took control in his hand of creating object and that is called inversion of control. Second how spring is doing it . Spring took definition in xml or in annotation from us that at runtime which object is need to be injected in which one at runtime and create that object using reflection at time of container loading.

Question 3 : What is the benefit I got using spring , I could used new keyword what is the issue with that.

Answer :  Now you will explain it by writing a code below.

You will say if I have an interface called PET .

Interface Pet{}

Now I have an implementation class Dog having name as “pet” (In annotation or XML where bean is defined ) for this Pet interface.

@Named("pet")Class Dog{}

Now I have one object called Manav and this manav has a pet as dog.

Class Manav{
@inject/@autowired@named("pet")/@resource("pet")
private Pet pet;
}

Now spring will define Dog object for Pet interface at runtime. But if we were using new keyword we have to define pet  = new Dog(); inside manav and in future manav does not need Dog he need Cat now then we have to replace Dog with Cat in code everywhere but with help of spring  we will remove @named(“pet”) from Dog class and put it at Cat class like below.

@Named("pet")
Class Cat{}

and spring will initialized Pet which Manav have with cat implementation without changing any code. Now he will be satisfied that you know why we are using spring IOC.

Question 4 : What are different type of IOC.

Answer :  Two types of IOC are present in spring.

  1. Constructor Based : When container called constructor with number of arguments and use that constructor to set associated objects  value with help of those arguments.
  2. Setter Based : Setter based DI happened when container call different setter methods of associated objects to initialize those objects if a non augmented constructor found in that bean.
  3. Field Injection : Field injection happen when we define @inject on field and does not want to define it’s setter and getter . Then spring container assign value to these fields directly through reflection without calling any method.

 

Question 5 : What are different stages of a bean(Life of a bean).

Answer :  Below steps are followed.

  1. Container will look bean definition in xml or annotations and initialize that bean through reflection.
  2. If an augmented constructor is found then it will call that constructor based on arguments given in beans.xml or else call their setter methods that is also based on beans.xml .
  3. If somebody used annotation then container will look out for annotated associated objects in that bean and initialize them according to their implementations.
  4. If a BeanPostProcessor is attached with that bean then container will call overridden processBeforeInitialization() method .If an init() method is specified then it will call. If a method with nay name but having @postprocessor annotation is present then it will be called.
  5. Now bean will be in container until container will feel that there is no live reference using this bean and let java garbage collector collect this bean.
  6. If bean has implemented DisposableBean interface then destroy method will be called before this bean will be destroyed. If a method with any name but having @predestroy annotation is present then it will be called.

 

Question 6 : Difference between BeanFactory and ApplicationContext.

Answer :

A BeanFactory is like a factory class that separate beans configurations ,their dependency from actual code and instantiates the bean whenever needed. XMLBeanFactory is one of the implementation of Beanactory which takes xml file as configuration data and instantiate beans according to that XML file.

BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml"));

ApplicationContext is same like BeanFactory except it have some more features like. It supports multiple files as config XML. It supports bean life cycle events. It supports internationalization etc.

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/application-context.xml");

ApplicationContext is preferred way to use spring but if project require only basic functionality of spring then BeanFactory can also be used.

Question 7 : Why beans are singleton in spring. Does it make them thread unsafe.

Answer :  Beans are singleton in spring . Well if we are using beans as functionality exposures( Means services who are exposing their methods) then it will not be thread safe but if we are using beans as data carriers and trying to modifying or performing some operation on that data then it is not thread safe. For that kind of functionality we should use prototype bean or commonpool functionality of spring.

Question 8 : In how many ways, we can provide configuration data to Spring.

Answer :  We can provide configuration data in three ways to spring framework so that spring can instantiate and inject beans into each other when required.

  1. XML based configuration(Beans.xml)
  2. Annotation based configuration(@autowired/@resources etc.)
  3. Java based configuration(@inject,@named)

Question 9 : What is XML based configuration and it’s example.

Answer :  In spring we can configure beans definition and their dependency on each other in XML files and register those files with help of BeanFactory or ApplicationContext while making application up as I explained above.

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    <!-- A simple bean definition -->   <bean id="..." class="...">       <!-- collaborators and configuration for this bean go here -->   </bean></beans>

Question 10 : What is Annotation and Java based configuration and it’s example.

Answer :  In spring we can configure beans definition and their dependency on each other in java source code with help of annotations. These annotations are of two types .

  1. Spring Based : These annotations are given by spring and present in spring framework jars .They can only be used when spring framework is present in application. These annotations majorly are
    @component(To define  a unique name for bean)
    @autowwired(To autowire bean into another bean)
    @resource(To define implementation of this bean which need to bean injected at object creation it match this with @component).
  2. Java Based : These are given by java and can be used with any inversion of control framework. Noo dependency on spring. These annotations majorly are
    @named(To define unique name)
    @inject(To inject bean into another).

Other related posts are :
String interview questions and answers
String vs StringBuffer vs StringBuilder
JPA Hibernate Sequence generator generating odd ID value and IntegrityConstraintVolation exception is coming
Logback logs are not working. Log4j logs are not working. Steps to make logging work.

Please share these posts on facebook,Linkeedin,Google+,Twitter by clicking on below icons.

Published by gauravtyagi77

How Are you All . My name is Gaurav Tyagi . I am from India . I am a Technology Lover.

14 thoughts on “Top 10 spring interview questions and their answers. Spring basic concepts.

Leave a reply to LEON Cancel reply