s
Home Sport & Games Online Games News Fashion Kids Sports Cloths Kids Vedio Game OTHERS Cricket Multiple Choice Question (MCQ) Multiple Choice Question (MCQ) Login

Spring Framework Java Interview Questions

Categories: JAVA

Q.1. What is autowiring in Spring? What are the autowiring modes?

 

Ans. Autowiring enables the programmer to inject the bean automatically. We don’t need to write explicit injection logic. Let’s see the code to inject bean using dependency injection.

 

Q.2. How to handle exceptions in Spring MVC Framework?

 

Ans. Spring MVC Framework provides the following ways to help us achieving robust exception handling.

 

(i) Controller Based:

We can define exception handler methods in our controller classes. All we need is to annotate these methods with @ExceptionHandler annotation.

 

(ii) Global Exception Handler:

Exception Handling is a cross-cutting concern and Spring provides @ControllerAdvice annotation that we can use with any class to define our global exception handler.

 

(iii) HandlerExceptionResolver implementation:

For generic exceptions, most of the times we serve static pages. Spring Framework provides HandlerExceptionResolver interface that we can implement to create global exception handler. The reason behind this additional way to define global exception handler is that Spring framework also provides default implementation classes that we can define in our spring bean configuration file to get spring framework exception handling benefits.

 

Q.3. What are some of the important Spring annotations which you have used?

 

Ans. Some of the Spring annotations that I have used in my project are:

 

(i) @Controller – for controller classes in Spring MVC project.

 

(ii) @RequestMapping – for configuring URI mapping in controller handler methods. This is a very important annotation, so you should go through Spring MVC RequestMapping Annotation Examples

 

(iii) @ResponseBody – for sending Object as response, usually for sending XML or JSON data as response.

 

(iv) @PathVariable – for mapping dynamic values from the URI to handler method arguments.

 

(v) @Autowired – for autowiring dependencies in spring beans.

 

(vi) @Qualifier – with @Autowired annotation to avoid confusion when multiple instances of bean type is present.

 

(vii) @Service – for service classes.

 

(viii) @Scope – for configuring the scope of the spring bean.

 

(ix) @Configuration, @ComponentScan and @Bean – for java based configurations.

 

(x) AspectJ annotations for configuring aspects and advices , @Aspect, @Before, @After, @Around, @Pointcut, etc.

 

Q.4. How to integrate Spring and Hibernate Frameworks?

 

Ans. We can use Spring ORM module to integrate Spring and Hibernate frameworks if you are using Hibernate 3+ where SessionFactory provides current session, then you should avoid using HibernateTemplate or HibernateDaoSupport classes and better to use DAO pattern with dependency injection for the integration.

 

Also, Spring ORM provides support for using Spring declarative transaction management, so you should utilize that rather than going for hibernate boiler-plate code for transaction management.

 

Q.5. Name the types of transaction management that Spring supports.

 

Ans. Two types of transaction management are supported by Spring. They are:

 

(i) Programmatic transaction management: In this, the transaction is managed with the help of programming. It provides you extreme flexibility, but it is very difficult to maintain.

(ii) Declarative transaction management: In this, transaction management is separated from the business code. Only annotations or XML based configurations are used to manage the transactions.

Spring Framework Java Interview Questions