Spring IoC Interview Questions
Spring IoC Interview Questions
What is Inversion of Control?
Inversion of Control (IoC) describes that a dependency injection need to be done by an external entity instead of creating the dependencies by the component itself. All the dependencies and relationships are created by IoC container. IoC is also known as dependency injection (DI).
What is Spring IoC container?
Spring IoC container is the basis for spring framework. The primary job of spring IoC container is to instantiate the java object, injecting its dependencies and managing them.
The object created and managed by spring core container is called a Spring Bean. The bean objects has to be added in the configuration metadata which is taken by the Spring IoC Controller. read more
What are the different Spring IoC containers?
The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework IoC container.
The org.springframework.beans.Bean.BeanFactory interface provides the end point for the spring core container to access the spring beans from container. BeanFactory provides mechanism can manage any type of object.
org.springframework.context.ApplicationContext is a sub-interface of BeanFactory. It adds integration with Spring AOP features, message resource handling, event propagation and application layer specific context.
What are the different types of Dependency injection?
Dependency injection (DI) is a process whereby objects define their dependencies, that is, the other objects they work with. Dependency Injection exists in two major variants
Constructor-based dependency injection
Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on other class. read more
Setter-based dependency injection
Setter injection uses the set* methods in a class file to garner property names that are configurable in the spring XML config. read more
What is autowiring?
Spring provides autowiring feature for connecting one bean with other bean automatically. We can inject beans using ‘ref’ in property or constructor-arg by yourself or we can use autowiring. To enable autowire for a bean autowire=”autowiretype” attribute should be added to bean. read more
What are the different modes of autowiring?
There are four different modes of autowiring in Spring
- no (default) – no autowiring
- byName – autowire matching name of the property to bean identifier (name or id)
- byType – autowire dependency by matching the property type
- constructor – same as byType(but this is for constructor dependency injection)
What is ‘lazy-init’ attribute of <bean> tag?
‘lazy-init’ is attribute of <bean> tag. It is used to specify whether a spring bean is to be lazily initialized or not. read more
Following are the valid values for lazy-init attribute
What are the different scopes available for configuration?
Spring Framework supports five scopes, three (request, session, globalsession) of which are available only if you use a webaware ApplicationContext (web applications).
What is a singleton bean?
A singleton bean is instantiated only once per spring IoC container. Spring IoC container creates one instance of a class for each singleton bean and returns the same object reference for all the requests made for the bean. read more
What is a prototype bean?
A prototype bean is instantiated every time when a request is made for the bean. Spring IoC container creates one instance returns the new object reference for each request. read more
<bean name="employeeService" class="com.answersz.scopes.EmployeeService" scope="prototype"/>
What is an Autowired Annotation in Spring?
Autowired Annotation is used to specify bean to automatically identify and wire dependency. @Autowired annotation wires the bean by matching the name or type. It is equivalent to bean’s autowire attribute in xml. read more
What is a Qualifier Annotation in Spring?
Qualifier annotation fine-tunes the autowiring. We can configure the qualifier value so the specific bean will be chosen and autowired if there are multiple beans present for the dependency class. read more
Articles related to Spring IoC Interview Questions