Java Spring Interview Questions

Want to become an expert in cracking Java Spring interview questions?

Start with practicing the questions below. Whether a question involves multiple choice or live coding, we will give you hints as you go and tell you if your answers are correct or incorrect.

After that, take our timed public Java Spring Interview Questions Test.

To use our service for testing candidates, buy a pack of candidates.

1. Movie Repository
Java Spring Database abstraction Public New

Consider the following Spring bean:

public class MovieRepository {

    private DataSource dataSource;

    public List<Movie> findMoviesByName(String name) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        return jdbcTemplate.query("select name, year, rating from movies where name like ?",
                new Object[]{name},
                new RowMapper<Movie>() {
                    public Movie mapRow(ResultSet resultSet, int i) throws SQLException {
                        return new Movie(resultSet.getString("name"),
                                resultSet.getInt("year"), resultSet.getInt("rating"));


Select all of the statements that are correct.

The jdbcTemplate will open a new connection to the database every time the code is executed.
The name parameter being passed unescaped to JdbcTemplate is a potential security problem.
As JdbcTemplate is thread-safe there is no need to create a new instance every time findMoviesByName() is called.
To inject a DataSource, the class needs to be annotated with @Repository.
A BeanPropertyRowMapper could be used to automatically map database rows to instances of the Movie class, provided the Movie class has the appropriate setter methods.

2. Task Executor
Java Spring Configuration Multithreading Public New

Consider the following Spring bean:

public class TaskExecutor {

    private int threadCount;
    private String threadNameTemplate;
    private AtomicInteger counter = new AtomicInteger();
    private ExecutorService executorService;

    public void init() {
        ThreadFactory threadFactory = (runnable) -> new Thread(threadNameTemplate + counter.incrementAndGet());
        executorService = Executors.newFixedThreadPool(threadCount, threadFactory);

Select all of the statements that are correct.

Properties injected with @Value may, among other sources, come from system properties and JVM arguments.
As the values of threadCount and threadNameTemplate are private and have no setters, they cannot be set by Spring.
Initialization will fail because the counter and executorService fields are not managed by Spring.
If a property named "thread.count" is not made available to Spring (i.e. through property files), the value of threadCount field will be 10.
The value of the field threadNameTemplate will always be set to the same value regardless of properties provided to Spring.

3. Weather Forecast
Java Spring Components Language features Public New

Consider the following two classes:

public class WeatherForecastService {

    private Thermometer thermometer;

    public WeatherForecastService(Thermometer thermometer) {
        this.thermometer = thermometer;

    @Scheduled(fixedDelay = 10000)
    public void takeTemperatureMeasurement() {

public class FakeThermometer implements Thermometer {

    public int measure() { return 10; };

Supposing Spring uses component scanning to discover beans, select all of the statements that are correct.

Instantiation of WeatherForecastService will fail because Thermometer is an interface and not a class.
Instantiation of WeatherForecastService will fail, because only @Repository beans may be injected into @Service beans.
Automatic execution of takeTemperatureMeasurement() every 10 seconds can be enabled by annotating the Spring configuration class with @EnableScheduling.
Automatic execution of takeTemperatureMeasurement() every 10 seconds can be enabled by using <task:annotation-driven/> in XML configuration.
A new instance of the FakeThermometer class will be created each time the takeTemperatureMeasurement() method is executed.

If you feel ready, take one of our timed public Java Spring Interview Questions tests:
  • Java and Spring Framework Online Test (Easy / Hard)
Not exactly what you are looking for? Go to our For Jobseekers section.