Top 25 spring rest interview questions and answers
What is the meaning of Dependency Injection?
Answer: It’s an aspect of Inversion of Control (IoC), which states that there is no need to create objects manually. Instead, it is important to mention how they are created. These design patterns help to remove the hard-coded dependencies and instead it injects dependency on Object. Dependency Injection makes unit testing easy, separation of concerns, boilerplate reduction and configurable components.
In this process, objects need to define their dependencies. The container injects these dependencies and creates the bean. The bean then controls the instantiation by using Service Locator or direct construction classes.
The advantage of using dependency injection is that it gives the configuration flexibility. The one thing, which is fixed, is the client’s behavior. It does not demand any changes in code behavior can be used as refactoring in legacy code. The high cohesion in DI help maximizes module reusability and minimizes module complexity.
While some of its disadvantages include difficulty in tracing the code, explosion of types, tight coupling and requiring configure details via construction code. If you know the whole thing about dependency injection, you’ realize later that most of your Spring Framework interview questions will be based on this topic only.
What is @Autowired Annotation?
Answer: The objective of @Autowired annotation is to give accurate control to know the location and method of autowiring. This is a type driven injection by default and find its uses in the autowire bean, constructor, etc. In case of dependency injection, @Autowired will first inform spring to look for a spring bean that will implement the required interface and put it into the setter. The @Autowired annotation is applied to the following:
@Autowired on property @Autowired on constructor @Autowired on the setter method You can activate this annotation by including context:annotation-config tag in the configuration file
What is Inversion of Control (IoC)?
Answer: IoC is a principle or a point during which the flow of the program is inverted. Rather, the external sources like framework and services and programmer control the flow of the program. As the name, Inversion of Control means inverting the control to get the loose coupling and creates the object with the new operator to container or framework. Now it’s the responsibility of the container to create an object as required.
In loose coupling, the @Autowired annotation over the reference and Spring Framework takes control to inject the dependency. While in tight coupling, the responsibility to create the dependency is on dependent class. This is again an important question that you’ll definitely find in your upcoming Spring Framework interview questions.
What is Spring Bean and what is its scope?
Answer: Spring Bean is a normal java class, which is initialized using the Spring IoC container. Spring ApplicationContext is used to fetch the Spring Bean instance. It is assembled, instantiated and managed by a Spring IoC container. Spring beans are created by using configuration metadata that you will pass to the container.
There are three ways to supply the configuration metadata to the Spring IoC container. These include XML-based configuration file, annotation-based configuration, and Java-based configuration.
Spring Framework supports the five scopes of a Spring BeanSingleton- uses only one bean instance for each container. In this scope, Spring Bean should not have shared instance variables as that may cause data inconsistency. Session- uses a new bean for each HTTP session. Global-session- creates global session beans for use in Portlet applications. Prototype- creates a new instance every time with the request for the bean. Request- similar to prototype scope but used for web applications
How can we inject beans in Spring Framework?
Answer: As this is a little practical concept, this question is often seen in the Spring Framework interview questions and answers for experienced users. Options like setter injection, constructor injection and field injection exists that helps to inject beans in the Spring Framework. However, the configuration is possible using XML file or annotations.What is Spring Bean Lifecycle?
Answer: Based on the definition of Java or XML, first, there is a need to create a spring instance. Spring Beans may require initialization to have it in a usable state. Once there is no requirement of bean, it is removed from the IoC container. Spring Framework helps in post-initialization and pre-destruction methods in Spring Beans. As this is the most popular Spring Framework Interview Questions among interviewers, we’re providing you with more information.
Once the bean is destroyed, there may be a need for some cleanup activity for the given bean. The collection of these activities is called the bean lifecycle.
We have seen Spring rest interview questions and answers.
0 Comments
Post a Comment