Interview
January 5, 2021

Spring Interview

What is the difference between ApplicationContext and BeanFactory in Spring framework?

Before seeing the difference between ApplicationContext and BeanFactory, let see some similarity between both of them. Spring provides two kinds of IOC containers, one is BeanFactory, and the other is ApplicationContext. Syntactically BeanFactory and ApplicationContext both are Java interfaces and ApplicationContext extends BeanFactory.

https://javarevisited.blogspot.com/2012/11/difference-between-beanfactory-vs-applicationcontext-spring-framework.html

What types of dependency injection are supported by Spring Framework? When do you use Setter and Constructor Injection, the pros and cons? (answer)

There are 2 types of dependency injection supported by Spring, constructor based injection, and setter-based injection.

Both types have their own advantages and disadvantages; you should use Constructor injection when an object's dependencies are not optional, and they must be initialized with their dependencies.

Also, use constructor injection if the order of initialization or dependency matters because, in Setter based injection, you cannot impose any order. Use setter injection when dependencies are optional.

https://javarevisited.blogspot.com/2012/11/difference-between-setter-injection-vs-constructor-injection-spring-framework.html

What does REST stand for? (answer)

REST stands for REpresentational State Transfer, which uses HTTP protocol to send data from client to server e.g. a book in the server can be delivered to the client using JSON or XML.

What is a resource? (answer)

A resource is how data is represented in REST architecture. By exposing entities as the resource it allows a client to read, write, modify, and create resources using HTTP methods like GET, POST, PUT, DELETE, etc.

What are safe REST operations? (answer)

REST API uses HTTP methods to perform operations. Some of the HTTP operations which don't modify the resource at the server are known as safe operations e.g. GET and HEAD. On the other hand, PUT, POST, and DELETE are unsafe because they modify the resource on the server.

What are idempotent operations? Why is idempotency important? (answer)

There are some HTTP methods e.g. GET which produce same response no matter how many times you use them e.g. sending multiple GET request to the same URI will result in same response without any side-effect hence it is known as idempotent.

On the other hand, the POST is not idempotent because if you send multiple POST requests, it will result in multiple resource creation on the server, but again, PUT is idempotent if you are using it to update the resource.

Even, multiple PUT requests to update a resource on a server will give the same end result.

What does @RequestMapping annotation do? (answer)

The @RequestMapping annotation is used to map web requests to Spring Controller methods. You can map requests based upon HTTP methods like the GET and POST and various other parameters

When do you need @ResponseBody annotation in Spring MVC? (answer)

The @ResponseBody annotation can be put on a method to indicates that the return type should be written directly to the HTTP response body (and not placed in a Model, or interpreted as a view name).

For example:

@RequestMapping(path = "/hello", method = RequestMethod.PUT)
@ResponseBody
public String helloWorld() {
 return "Hello World";
}

Alternatively, you can also use @RestController annotation instead of @Controller annotation. This will remove the need for using @ResponseBody because as discussed in the previous answer, it comes automatically with @RestController annotation.

What does @PathVariable do in Spring MVC? Why it's useful in REST with Spring? (answer)

It's one of the useful annotations from Spring MVC which allows you to read values from URI like query parameter. It's particularly useful in case of creating RESTful web service using Spring because in REST resource identifiers are part of URI.

Where do you need @EnableWebMVC? (answer)

The @EnableWebMvc annotation is required to enable Spring MVC when Java configuration is used to configure Spring MVC instead of XML. It is equivalent to <mvc: annotation-driven> in XML configuration.