Table of Contents

1. Overview

In this tutorial, we’re going to look at some of the most common Spring-related questions that might pop up during a job interview.

Or we can load one XML file that will contain all other configs:

ApplicationContext context = new ClassPathXmlApplicationContext("spring-all.xml");

And inside this XML file we’ll have the following:

<import resource="main.xml"/>
<import resource="scheduler.xml"/>

Q15. What Is Spring Security?

Spring Security is a separate module of the Spring framework that focuses on providing authentication and authorization methods in Java applications. It also takes care of most of the common security vulnerabilities such as CSRF attacks.

To use Spring Security in web applications, we can get started with the simple annotation @EnableWebSecurity.

For more information, we have a whole series of articles related to security.

Q16. What Is Spring Boot?

Spring Boot is a project that provides a pre-configured set of frameworks to reduce boilerplate configuration. This way, we can have a Spring application up and running with the smallest amount of code.

Q17. Name Some of the Design Patterns Used in the Spring Framework?

  • Singleton Pattern – singleton-scoped beans
  • Factory Pattern – Bean Factory classes
  • Prototype Pattern – prototype-scoped beans
  • Adapter Pattern – Spring Web and Spring MVC
  • Proxy Pattern – Spring Aspect-Oriented Programming support
  • Template Method Pattern – JdbcTemplate, HibernateTemplate, etc.
  • Front Controller – Spring MVC DispatcherServlet
  • Data Access Object – Spring DAO support
  • Model View Controller – Spring MVC

Q18. How Does the Scope Prototype Work?

Scope prototype means that every time we call for an instance of the Bean, Spring will create a new instance and return it. This differs from the default singleton scope, where a single object instance is instantiated once per Spring IoC container.

3. Spring Web MVC

Q19. How to Get ServletContext and ServletConfig Objects in a Spring Bean?

We can do either by implementing Spring-aware interfaces. The complete list is available here.

We could also use @Autowired annotation on those beans:

@Autowired
ServletContext servletContext;

@Autowired
ServletConfig servletConfig;

Q20. What Is a Controller in Spring MVC?

Simply put, all the requests processed by the DispatcherServlet are directed to classes annotated with @Controller. Each controller class maps one or more requests to methods that process and execute the requests with provided inputs.

To take a step back, we recommend having a look at the concept of the Front Controller in the typical Spring MVC architecture.

Q21. How Does the @RequestMapping Annotation Work?

The @RequestMapping annotation is used to map web requests to Spring Controller methods. In addition to simple use cases, we can use it for mapping of HTTP headers, binding parts of the URI with @PathVariable, and working with URI parameters and the @RequestParam annotation.

More details on @RequestMapping are available here.

For more Spring MVC questions, please check out our article on Spring MVC interview questions.

4. Spring Data Access

Q22. What Is Spring JdbcTemplate Class and How to Use It?

The Spring JDBC template is the primary API through which we can access database operations logic that we’re interested in:

  • Creation and closing of connections
  • Executing statements and stored procedure calls
  • Iterating over the ResultSet and returning results

In order to use it, we’ll need to define the simple configuration of DataSource:

@Configuration
@ComponentScan("org.baeldung.jdbc")
public class SpringJdbcConfig {
    @Bean
    public DataSource mysqlDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/springjdbc");
        dataSource.setUsername("guest_user");
        dataSource.setPassword("guest_password");
 
        return dataSource;
    }
}

For further explanation, check out this quick article.

Q23. How to Enable Transactions in Spring and What Are Their Benefits?

There are two distinct ways to configure Transactions — with annotations or by using Aspect-Oriented Programming (AOP) — each with their advantages.

Here are the benefits of using Spring Transactions, according to the official docs:

  • Provide a consistent programming model across different transaction APIs such as JTA, JDBC, Hibernate, JPA and JDO
  • Support declarative transaction management
  • Provide a simpler API for programmatic transaction management than some complex transaction APIs such as JTA
  • Integrate very well with Spring’s various data access abstractions

Q24. What Is Spring DAO?

Spring Data Access Object (DAO) is Spring’s support provided to work with data access technologies like JDBC, Hibernate and JPA in a consistent and easy way.

There is an entire series discussing persistence in Spring that provides a more in-depth look.

5. Spring Aspect-Oriented Programming

Q25. What Is Aspect-Oriented Programming (AOP)?

Aspects enable the modularization of cross-cutting concerns such as transaction management that span multiple types and objects by adding extra behavior to already existing code without modifying affected classes.

Here is the example of aspect-based execution time logging.

Q26. What Are Aspect, Advice, Pointcut and JoinPoint in AOP?

  • Aspect – a class that implements cross-cutting concerns, such as transaction management
  • Advice – the methods that get executed when a specific JoinPoint with matching Pointcut is reached in the application
  • Pointcut – a set of regular expressions that are matched with JoinPoint to determine whether Advice needs to be executed or not
  • JoinPoint – a point during the execution of a program, such as the execution of a method or the handling of an exception

Q27. What Is Weaving?

According to the official docs, weaving is a process that links aspects with other application types or objects to create an advised object. This can be done at compile time, load time, or runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.

6. Spring 5

Q28. What Is Reactive Programming?

Reactive programming is about non-blocking, event-driven applications that scale with a small number of threads, with back pressure being a key ingredient that aims to ensure producers don’t overwhelm consumers.

These are the primary benefits of reactive programming:

  • Increased utilization of computing resources on multicore and multi-CPU hardware
  • Increased performance by reducing serialization

Reactive programming is generally event-driven, in contrast to reactive systems, which are message-driven. So, using reactive programming does not mean we’re building a reactive system, which is an architectural style.

However, reactive programming may be used as a means to implement reactive systems if we follow the Reactive Manifesto, which is quite vital to understand.

Based on this, reactive systems have four important characteristics:

  • Responsive – The system should respond in a timely manner.
  • Resilient – In case the system faces any failure, it should stay responsive.
  • Elastic – Reactive systems can react to changes and stay responsive under varying workload.
  • Message-driven – Reactive systems need to establish a boundary between components by relying on asynchronous message passing.

Q29. What Is Spring WebFlux?

Spring WebFlux is Spring’s reactive-stack web framework, and it’s an alternative to Spring MVC.

In order to achieve this reactive model and be highly scalable, the entire stack is non-blocking. Check out our tutorial on Spring 5 WebFlux for additional details.

Q30. What Are the Mono and Flux Types?

The WebFlux framework in Spring Framework 5 uses Reactor as its async foundation.

This project provides two core types: Mono to represent a single async value and Flux to represent a stream of async values. They both also implement the Publisher interface defined in the Reactive Streams specification.

Mono implements Publisher and returns 0 or 1 elements:

public abstract class Mono<T> implements Publisher<T> {...}

And Flux implements Publisher and returns N elements:

public abstract class Flux<T> implements Publisher<T> {...}

By definition, the two types represent streams, and so they’re both lazy. This means nothing is executed until we consume the stream using the subscribe() method. Both types are also immutable, so calling any method will return a new instance of Flux or Mono.

Q31. What Is the Use of WebClient and WebTestClient?

WebClient is a component in the new Web Reactive framework that can act as a reactive client for performing non-blocking HTTP requests. Since it’s reactive client, it can handle reactive streams with back pressure, and it can take full advantage of Java 8 lambdas. It can also handle both sync and async scenarios.

On the other hand, the WebTestClient is a similar class that we can use in tests. Basically, it’s a thin shell around the WebClient. It can connect to any server over an HTTP connection. It can also bind directly to WebFlux applications using mock request and response objects, without the need for an HTTP server.

Q32. What Are the Disadvantages of Using Reactive Streams?

There are some major disadvantages to using reactive streams:

  • Troubleshooting a Reactive application is a bit difficult, so be sure to check out our tutorial on debugging reactive streams for some handy debugging tips.
  • There is limited support for reactive data stores since traditional relational data stores have yet to embrace the reactive paradigm.
  • There’s an extra learning curve when implementing.

Q33. Is Spring 5 Compatible With Older Versions of Java?

In order to take advantage of Java 8 features, the Spring codebase has been revamped. This means older versions of Java cannot be used. So, the framework requires a minimum of Java 8.

Q34. How Does Spring 5 Integrate With JDK 9 Modularity?

In Spring 5, everything has been modularized. This way, we won’t be forced to import jars that may not have the functionalities we’re looking for.

Please have a look at our guide to Java 9 modularity for an in-depth understanding of how this technology works.

Let’s see an example to understand the new module functionality in Java 9 and how to organize a Spring 5 project based on this concept.

We’ll first create a new class that contains a single method to return a String “HelloWorld”. We’ll place this within a new Java project — HelloWorldModule:

package com.hello;
public class HelloWorld {
    public String sayHello(){
        return "HelloWorld";
    }
}

Then we create a new module:

module com.hello {
    export com.hello;
}

Now let’s create a new Java Project, HelloWorldClient, to consume the above module by defining a module:

module com.hello.client {
    requires com.hello;
}

The above module will be available for testing now:

public class HelloWorldClient {
    public static void main(String[] args){
        HelloWorld helloWorld = new HelloWorld();
        log.info(helloWorld.sayHello());
    }
}

Q35. Can We Use Both Web MVC and WebFlux in the Same Application?

As of now, Spring Boot will only allow either Spring MVC or Spring WebFlux, as Spring Boot tries to auto-configure the context depending on the dependencies that exist in its classpath.

Also, Spring MVC cannot run on Netty. Moreover, MVC is a blocking paradigm and WebFlux is a non-blocking style. So, we shouldn’t be mixing both together because they serve different purposes.

7. Conclusion

In this extensive article, we’ve explored some of the most important questions for a technical interview all about Spring.

We hope that this article will help in upcoming Spring interviews. Good luck!