0% found this document useful (0 votes)
21 views

Sample Grokking the Spring Boot Interview

The document is a comprehensive guide for preparing for Spring Boot interviews, covering essential topics such as the Spring Core Framework, Dependency Injection, Spring Bean Lifecycle, Aspect Oriented Programming (AOP), Spring MVC, REST, and Spring Boot features. It includes a detailed table of contents with specific questions and explanations related to each topic. The guide aims to equip candidates with the necessary knowledge and understanding to succeed in Spring Boot interviews.

Uploaded by

way2ashutosh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Sample Grokking the Spring Boot Interview

The document is a comprehensive guide for preparing for Spring Boot interviews, covering essential topics such as the Spring Core Framework, Dependency Injection, Spring Bean Lifecycle, Aspect Oriented Programming (AOP), Spring MVC, REST, and Spring Boot features. It includes a detailed table of contents with specific questions and explanations related to each topic. The guide aims to equip candidates with the necessary knowledge and understanding to succeed in Spring Boot interviews.

Uploaded by

way2ashutosh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

by Javih v#javihgâ Sold

to
Table Of Contents
Overview 2
Why Prepare For The Spring Boot Interview? 5
How to Prepare for Spring Boot Interviews? 8
Topics 11
Spring Core Framework 13
1. W
hat is the Spring framework? Why should Java
programmers use the Spring framework? 15
2. What is the Dependency Injection design pattern? 16
3. What is the Inversion of Control concept, how does
Spring support IOC? 16
4. How do you configure Spring Framework? 18
5. Can we use more than one configuration file for our
Spring project?
18
6. What types of dependency injection are supported
by Spring Framework? When do you use Setter and
Constructor Injection, the pros and cons?

18
7.Difference between the setter and constructor injection
in Spring?
19
8. Difference between Factory Pattern and Dependency
Injection in Java? 21
9. What are the different modules in spring? 21
10. What is the difference between Spring MVC and Spring
core? 21
11. What is
AOP? How is it implemented in Spring? 22
12. What is
Advice in AOP? 23
13. What are
the joint Point and point cut? 23
14. What is
component scanning in Spring Framework? 23
Container, Dependency, and IOC 25
1. W
hat is dependency injection and what are the
advantages of using it? 26
2. What is an interface and what are the advantages of
making use of them in Java?
27
3. What is an ApplicationContext in Spring? 28
4. How are you going to create a new instance of an
ApplicationContext? 29
5. Can you describe the lifecycle of a Spring Bean in an
ApplicationContext? 31
6. How are you going to create an ApplicationContext in an
integration test?
33
7.What is the preferred way to close an application
context? Does Spring Boot do this for you? 34
8. What is Dependency injection using Java configuration?
35
9. What is Dependency injection using annotations (@
Autowired)?–Component scanning, Stereotypes?–Scopes
for Spring beans? 35
10. What is
the default scope? 35
11. Ar
e beans lazily or eagerly instantiated by default? How
do you alter this behavior? 35
12. What is a property source? How would you use @
PropertySource? 36
13. What is a BeanFactoryPostProcessor and what is it used
for? When is it invoked? 36
14. W
hy would you define a static @Bean method when
creating your own BeanFactoryPostProcessor?
37
15. What is a PropertySourcesPlaceholderConfigurer used
for? 37

16. What is a BeanPostProcessor and how is it


different to a BeanFactoryPostProcessor? What do
they do? When are
they called? 38
17. What is an initialization method and how is it declared
on a Spring bean? 38
18. W
hat is a destroy method, how is it declared and when is
it called? 38
19. C
onsider how you enable JSR-250 annotations like @
PostConstruct and @PreDestroy? When/how will they
get called?
40
20.How else can you define an initialization or destruction
method for a Spring bean? 41
21. What
does component-scanning do? 41
22.What is the behavior of the annotation @Autowired
with regards to field injection, constructor injection and
method injection? 42
23.How does the @Qualifier annotation complement the
use of @Autowired? 44
24.What is a proxy object and what are the two different
types of proxies Spring can create?
45
25.What are the limitations of these proxies (per type)? 46
26. What is the power of a proxy object and where are
the disadvantages? 46
27.What is the difference between @Autowired and
@Inject annotation in Spring?
47
Spring Bean Lifecycle 49
1. What is a
bean in Spring Framework? 50
2. What is the default scope of bean in the Spring
framework?
50
3. Do Spring singleton beans are thread- safe? 50

4. What is the difference between a singleton and


prototype bean?
50
5: Explain the Spring Bean-LifeCycle? 51
6. What is Bean Factory, have you used XMLBeanFactory? 53
7.What is the difference between ApplicationContext and
BeanFactory in Spring framework? 53
8. What does the @Bean annotation do in Spring? 54
9. What is the default bean id if you only use @Bean? 55
10. How can
you override this? 55
11. W
hy are you not allowed to annotate a final class with @
Configuration?
56
12. How do @Configuration annotated classes support
singleton beans? 56
13. Why can’t
@Bean methods be final either? 56
14. How do
you configure profiles? 56
15. W
hat are possible use cases where they might be useful?
57
16. Can you
use @Bean together with @Profile? 58
17. C
an you use @Component together with @Profile
annotation?
58
18. How many
profiles can you have? 59
19. How do you inject scalar/literal values into Spring
beans? 59
20.What is @Value used for? 60
21. What is
Spring Expression Language (SpEL for short)? 60
22.What is the Environment abstraction in Spring? 61
23.Where can properties in the environment come from? 61
24.What can you reference using SpEL? 61
25.What is the difference between $ and # in @Value
expressions? 62

Aspect Oriented Programming (AOP) 63


1. What is
the concept of AOP? 66
2. Which problem AOP solves? 66
3. What is a cross-cutting concern? 66
4. Can you name three typical cross-cutting concerns? 66
5. What two problems arise if you don’t solve a cross-
cutting concern via AOP? 67
6. What is a pointcut, a join point, advice, an aspect,
weaving?
67
7.How does Spring solve (implement) a cross-cutting
concern? 68
8. What are the two proxy-types used in Spring AOP? 69
9. What are the limitations of the two proxy-types used in
Spring AOP? 70
10. What visibility must Spring bean methods have to be
proxied using Spring AOP? 71
10. H
ow many advice types do Spring support? Can you
name each one?
71
11. What are
they used for? 72
12. W
hich types of advice you can use to try and catch
exceptions?
73
13. What is
the JoinPoint argument used for? 73
15. What is a ProceedingJoinPoint? Which advice type is it
used with? 74
16. C
an you name some popular Aspect-oriented
programming libraries?
75
17. What are the different types of Weaving which is
available in AOP? 75
Spring MVC 77
1. M
VC is an abbreviation for a design pattern. What does it
stand for and what is the idea behind it?
79
2. Do you need spring-mvc.jar in your classpath or is it part
of spring-core? 80
3. What is the DispatcherServlet and what is it used for? 80
4. Is the DispatcherServlet instantiated via an application
context? 81
5. What is the root application context in Spring MVC? How
is it loaded? 82
6. What is the @Controller annotation used for? How can
you create a controller without an annotation? 83
7.What is the ContextLoaderListener and what does it do?
83
8. What are you going to do in the web.xml? Where do you
place it? 84
9. How is an incoming request mapped to a controller and
mapped to a method? 84
10. What is
the @RequestParam used for? 85
11. W
hat are the differences between @RequestParam and
@PathVariable? 86
12. What are some of the valid return types of a controller
method? 87
13. What is a View and what’s the idea behind supporting
different types of View? 88
14. How is the right View chosen when it comes to the
rendering phase? 88
15. What is the
Model in Spring MVC Framework? 89
16. Why do you have access to the model in your View?
Where does it come from? 89
17. What is
the purpose of the session scope? 90
18. What is
the default scope in the web context? 90
19. Why are
controllers testable artifacts? 90
20.What does the InternalResourceViewResolver do? 90
21. W
hat is Spring MVC? Can you explain How one request is
processed?
91
22. What is the ViewResolver pattern? how it works in
Spring MVC
92
23.Explain Spring MVC flow with a simple example like
starting from Container receives a request and forward
to your Java application?
92
24. If a user checked in CheckBox and got a validation
error in other fields and then he unchecked the
CheckBox, what would be the selection status in the
command object in Spring MVC? How do you fix this
issue? 96
25. What are the different implementations of the View
interface you have used in Spring MVC?
97
26. What is the use of DispatcherServlet in Spring MVC? 97
27.What is the role of InternalResourceViewResolver in
Spring MVC 98
28.Difference between @RequestParam and @
PathVariable in Spring MVC? 100
29.Difference between @Component, @Service, @
Controller, and @Repository annotations in Spring MVC?
102
REST 105
1. What does
REST stand for? 107
2. What is a resource? 107
3. What are safe REST operations? 107
4. What are idempotent operations? Why is idempotency
important? 107
5. Is REST scalable and/or interoperable? 108
6. What are the advantages of the RestTemplate? 108
7.Which HTTP methods does REST use? 109
8. What is an HttpMessageConverter in Spring REST? 109

9. How to create a custom implementation of


HttpMessageConverter to support a new type of
request/ response? 110
10. Is REST
normally stateless? 110
11. What does
@RequestMapping annotation do? 110
12. Is
@Controller a stereotype? Is @RestController a stereotype?
111
13. W
hat is the difference between @Controller and @
RestController? 111
14. W
hen do you need @ResponseBody annotation in Spring
MVC? 112
15. What does @PathVariable do in Spring MVC? Why is it
useful in REST with Spring? 112
16. What is the HTTP status return code for a successful
DELETE statement? 113
17. What does
CRUD mean? 113
18. Where do
you need @EnableWebMVC annotation? 114
19. W
hen do you need @ResponseStatus annotation in Spring
MVC? 114
20.Is REST secure? What can you do to secure it? 115
21. Does REST
work with transport layer security (TLS)? 116
22.Do you need Spring MVC in your classpath for
developing RESTful Web Service? 116
Spring Boot Intro 118
1. What is
Spring Boot? Why should you use it? 120
2. What is the advantage of using Spring Boot? 121
3. What is the difference between Spring Boot and Spring
MVC? 121
4. What is the difference between Core Spring and
Spring Boot? 122
5. What are some important features of using Spring Boot?
122
6. What is auto-configuration in Spring boot? How does it
help? Why is Spring Boot called opinionated? 124
7.What is starter dependency in Spring Boot? How does it
help? 125
8. What is the difference between @SpringBootApplication
and @EnableAutoConfiguration annotation? 125
9. What is Spring Initializer? why should you use it? 126
10. What is a Spring Actuator? What are its advantages?
127
11. What is
Spring Boot CLI? What are its benefits? 128
12. Where do you define properties in Spring Boot
application? 129
13. Ca
n you change the port of the Embedded Tomcat server
in Spring boot? If Yes, How? 129
14. W
hat is the difference between an embedded container
and a WAR? 130
15. What embedded containers does Spring Boot support?
130
16. What are
some common Spring Boot annotations? 130
17. Can you name some common Spring Boot Starter POMs?
131
18. Can you
control logging with Spring Boot? How? 131
19. Difference between @SpringBootApplication and @
EnableAutoConfiguration annotations in Spring Boot? 132
20.What is the difference between
@ContextConfiguration and
@SpringApplicationConfiguration in Spring Boot
Testing? 132
21. Where does Spring Boot look for application.properties
file by default? 133
22.How do you define profile specific property files? 133
Spring Boot Auto Configuration 134
1. What is
Spring Boot auto-configuration? 135
2. How does auto-configuration work? How does it know
what to configure? 136
3. What are some common Spring Boot annotations? 136
4. What does @EnableAutoConfiguration annotation do?
143
5. How does Spring Boot auto-configuration works? 144
6. What does @SpringBootApplication do? 144
7.Does Spring Boot do component scanning? Where does it
look by default? 146
8. How are DataSource and JdbcTemplate auto-
configured? 147
9. What is the purpose of spring.factories? 148
10. How do you customize Spring Boot auto configuration?
148
11. How to create your own auto-configuration in Spring
Boot? 149
12. W
hat are the examples of @Conditional annotations? How
are they used? 150
Spring Boot Starter 152
1. W
hat is starter dependency in Spring Boot? What is the
advantage of it? 153
2. How do you define properties in Spring Boot? Where? 154
3. What does @SpringBootApplication annotation do? 155
4. What things affect what Spring Boot sets up? 155
5. What does spring boot starter web include? 156
6. Can you make your own custom starter dependency? 156
7.What are some common Spring Boot Starter
dependencies? Can you name a few? 157
8. How do you add a Spring boot starter in your project? 157
9. Which Spring Boot starter will you add to enable Spring
boot testing and relevant libraries? 158
10. What is
Spring Boot Starter Parent? 158
Spring Boot Actuator 160
1. What is the
Spring Boot Actuator? 162
2. What are the different ways Actuator provides to gain
insight into a Spring Boot application? 162
3. Why do you need to secure Spring Boot Actuator’s
endpoints? 163
4. How do you secure the Spring Boot Actuator’s endpoint
to restrict access? 163
5. What value does Spring Boot Actuator provide? 163
6. What are the two protocols you can use to access
actuator endpoints? 164
7.What are the actuator endpoints that are provided out
of the box? 164
8. What is the info endpoint for? How do you supply data?
165
9. How do you change the logging level of a package
using the logger’s endpoint? 166
10. How do
you access an endpoint using a tag? 167
11. What are
metrics for? 168
12. How do
you create a custom metric? 168
13. What is a
Health Indicator in Spring Boot? 169
14. W
hat are the Health Indicators that are provided out of
the box? 169
15. What is the
Health Indicator status? 170
16. W
hat are the Health Indicator statuses that are provided
out of the box? 170
17. H
ow do you change the Health Indicator status severity
order? 171
Spring Boot CLI 173
1. What is
Spring Boot CLI? 174
2. Can you write a Spring application with Groovy? 175
3. What are the main advantages of the Spring Boot
command-line interface (CLI)? 175
4. What does @Grab annotation do? When to use this? 175
5. What is Spring Initializer? 176
6. How does Spring Boot CLI resolve dependencies? 176
Spring Testing 178
1. How to
define a testing class in Spring? 180
2. Which starter package do you need to test the spring
boot application? 181
3. What type of tests typically use Spring? 181
4. What are the three common Spring boot test
annotations? 181
5. How can you create a shared application context in a
JUnit integration test? 182
6. When and where do you use @Transactional in testing?
182
7.How are mock frameworks such as Mockito or EasyMock
used in Spring Boot? 183
8. How is @ContextConfiguration used in Spring Boot? 184
9. How does Spring Boot simplify writing tests? 185
10. What does @SpringBootTest do?
How does it interact with
@SpringBootApplication and @
SpringBootConfiguration? 186
11. When do you want to use @SpringBootTest annotation?
186
12. What does @SpringBootTest auto-configure? 187
13. What dependencies does the spring-boot-starter-test
bring to the classpath? 188
14. How do you perform integration testing with @
SpringBootTest for a web application? 188
15. When do you want to use @WebMvcTest? What does it
auto-configure? 188
16. What are the differences between @MockBean and @
Mock annotations? 189
17. When do you want @DataJpaTest for? What does it
auto-configure? 189
18. What is the use of @DirtiesContext annotation while
Testing Spring Boot application? 191
19. W
hat is the difference between @ContextConfiguration and
@SpringApplicatoinConfiguration in Spring Boot testing?
191
20.What is the difference between @ContextConfiguration
and @SpringBootTest? 192
Data Management And JDBC 193
1. W
hat is the difference between checked and unchecked
exceptions? 194
2. Why does Spring prefer unchecked exceptions? 195
3. What is the Spring data access exception hierarchy? 195
4. How do you configure a DataSource in Spring? 196
5. What is the Template design pattern and what is the
JDBC template? 197
6. What is a callback? 197
7.What are the JdbcTemplate callback interfaces that can
be used with queries? 198
8. What is each used for? (You would not have to
remember the interface names in the exam, but you
should know what they do if you see them in a code
sample). 199
9. Can you execute a plain SQL statement with the JDBC
template? 200
10. Wh
en does the JDBC template acquire (and release) a
connection, for every method called or once per
template? Why? 200
11. How do the
JdbcTemplate support queries? 201
12. How does it return objects and lists/maps of objects? 202
13. Wh
at is a transaction? What is the difference between a
local and a global transaction? 203
14. Is a
transaction a cross-cutting concern? How is it
implemented by Spring? 204
15. How are you going to define a transaction in Spring? 204
16. What does
@Transactional do? 204
17. What is the
PlatformTransactionManager? 204
18. Is the JDBC template able to participate in an existing
transaction? 205
19. What is @EnableTransactionManagement for? 206
20.How does transaction propagation work? 206
21. Wh
at happens if one @Transactional annotated method is
calling another @Transactional annotated method
inside the same object instance? 208
22.Where can the @Transactional annotation be used? 209
23.What is a typical usage if you put it at the class level? 210
24.What does declarative transaction management
mean?• What is the default rollback policy? How can
you override it? 210
25.What is the default rollback policy in a JUnit test,
when you use the
@RunWith(SpringJUnit4ClassRunner.class)
in JUnit 4 or @ExtendWith(SpringExtension.class) in
JUnit 5, and annotate your @Test annotated method
with @
Transactional? 212
Spring Data JPA 214
1. What is
JPA? 216
2. What are some advantages of using JPA? 216
3. What is the Spring data repository? 217
4. What is the naming convention for finder methods in the
Spring data repository interface? 217
5. Why is an interface not a class? 217
6. Can we perform actual tasks like access, persist, and
manage data with JPA? 217
7.How can we create a custom repository in Spring data
JPA? 218
8. What is PagingAndSortingRepository? 218
9. What is @Query used for? 218
10. Give an example of using @Query annotation with JPQL.
218
11. Can you
name the different types of entity mapping. 219
12. Define entity and name the different properties of an
entity. 219
13. What is
PlatformTransactionManager? 219
14. How can
we enable Spring Data JPA features? 220
15. Differentiat
e between findById() and getOne(). 220
16. Are you able to participate in a given transaction in
Spring while working with JPA? 221
17. W
hich PlatformTransactionManager(s) can you use with
JPA? 221
18. What do you have to configure to use JPA with Spring?
How does Spring Boot make this easier? 221
19. How are Spring Data JPA Repositories implemented by
Spring at runtime? 222
20.What type of transaction Management Spring support?
223
21. Ho
w do you call a stored procedure by using the Spring
framework? 224
22.What do the JdbcTemplate and JmsTemplate class
offer in Spring? 224
Spring Cloud 225
1. Explain
Spring cloud? or, What is Spring Cloud? 227
2. What are the common features of Spring cloud? 227
3. Explain load balancing? or What is load balancing? 227
4. How load balancing is implemented in Spring cloud? 228
5. What is the meaning of Service registration and
discovery? 228
6. What is Hystrix? 228
7.Explain Netflix feign? Or What is Netflix feign? 229
8. Why do we use Netflix feign? 229
9. What is the use of the Spring cloud bus? 229
10. What are
the advantages of Spring cloud? 229
11. What is
PCF? 230
12. What is the
purpose of the Hystrix circuit breaker? 230
13. Na
me the services that provide service registration and
discovery. 230
14. Give the
benefits of Eureka and Zookeeper? 230
15. Wh
at is the major difference between Spring Cloud and
Spring boot? 230
16. What are
some common Spring cloud annotations? 231
Spring Security 232
1.Spring Security Basics Interview questions 234
1. What is
Spring Security? 234
2. What is the delegating filter proxy in Spring Security? 235
3. What are some restrictions on using delegating filter
proxy in Spring security? 236
4. Do Fitler’s life-cycle methods like init() and destroy()
will be a delegate to the target bean by
DelegatingFilterProxy?
237
5. Who manages the life-cycle of filter beans in Spring? 237
6. What is the security filter chain in Spring Security? 238
7.What are some predefined filters used by Spring Security?
What are their functions and in which order they occurred?
239
8. Can you add custom filters in Spring security’s filter
chain? 240
9. How to implement a custom filter in Spring Security? 240
10. How to add a custom filter into the Spring Security filter
chain? 240
11. Is
security a cross-cutting concern? How is it implemented
internally? 242
12. Wh
at does @ and # is used for in Spring Expression
Language (EL)? 242
13. Which security annotations are allowed to use SpEL?
242
14. What is a
security context in Spring? 243
2. Spring Security Authentication and Authorization Interview
Questions 244
15. What are authentication and authorization? Which must
come first? 244
16. What is a
Principal in Spring Security? 245
17. Why do you
need the intercept-url? 245
18. Why do you
need method security? 247
19. Is
it enough to hide sections of my output (e.g. JSP- Page)?
248
20.What type of object is typically secured at the method
level (think of its purpose, not its Java type). 248
21. In which order do you have to write multiple intercept-
urls? 248
22.What do @Secured and @RolesAllowed do? What is
the difference between them? 249
3. Spring Security Password Encoding questions 250
23.Does Spring Security support password hashing?
What is salting? 250
24.What is PasswordEncoder? 251
25.What are some implementations of 252
PasswordEncoder in Spring Security? 252
26. How do you control concurrent Sessions on Java web
applications using Spring Security? 252
27.How do you set up LDAP Authentication using Spring
Security? 254
28. How to implement Role-Based Access Control (RBAC)
using Spring Security? 259
Conclusion 261
Index 264
Grokking the Spring Boot Interview
© 2021, Javin Paul

Version 1.0 - January 2021

All rights reserved. This book or any portion


thereof may not be reproduced or used in any
manner whatsoever without the express written
permission of the publisher except for the use of
brief quotations in a book review.

GROKKING THE SPRING BOOT 1


Overview

GROKKING THE SPRING BOOT 2


Java Interviews are notoriously tough, not
because they ask impossible algorithmic
questions like Google or Amazon but because of
the vast nature of Java API, Frameworks and
Libraries. ! It’s not enough for you to just know
Java and expect that you will be able to clear
Java Interviews.

To pass the Java Interviews, you should also


know essential Java frameworks like Spring,
Spring Boot, and Hibernate. In my previous
book, Grokking the Java Interview, I touched
upon essential core Java topics like Collections,
Multithreading, and Java Fundamentals and in
this book, I have shared common Spring
Framework questions from Job interviews.

Spring Framework is the most popular and


almost a standard framework for developing
Java applications, both core java as well as
Java web application which runs on servers like
Tomcat.

Like Java, Spring Framework is also very vast


and there are several sub-projects like
Containers, Core Spring Concepts like IoC and
Dependency Injection, Spring MVC, Spring Boot,
Spring Data JPA, Testing, and miscellaneous
Spring APIs.

This book touches base on all of them and has


questions to test your knowledge about those
topics. You can use these questions to revise
those essential Spring concepts in quick time
and you can also use these questions to explore
Spring Framework and Spring Boot further.

This book is also useful for Java developers

GROKKING THE SPRING BOOT 3


who are preparing for the Spring Professional
Certification Exam as I have tried to answer
most of the questions from the Official Spring
Certification Exam guide. This means you can
use this as a review study guide for your spring
certification preparation as well.

I also have a spring certification practice test


on Udemy where I have shared 250+ high
quality questions on Spring Boot, and Spring
Security and other spring certification topics.
If you are preparing for Spring certification
you can use that to build the speed and
accuracy required to prepare for the exam.

Remember, Spring Professional certification is not


easy as it requires 76% to pass the actual exam
which is very hard in limited time, especially if you
haven’t practiced before. That’s the reason
many experienced Java and Spring developers
fail to clear the exam in the first attempt.

GROKKING THE SPRING BOOT 4


How to
Prepare for
Spring Boot
Interviews?

GROKKING THE SPRING BOOT 8


In order to best prepare for Spring Boot
Interviews, you need to have a solid
understanding of Spring Framework and their
different sub-projects like Spring MVC, REST,
Spring Data JPA, Testing, Spring Cloud etc.

If you have worked in Spring Boot projects then


there is a good chance that you are already
familiar with key concepts like auto-configuration,
actuator, and starter dependencies. Those are
must and if you haven’t used them, I strongly
suggest you to write a Spring boot program, or
create a Spring boot project from scratch.

If you need help, you can find many Spring


Boot projects on Youtube or you can join this
Udemy course.

Once you have some experience under your


belt, you can go through each topic and try to
answer questions on your own. If you don’t
know, look at the answer and then research it
to learn better.
This is the best approach to prepare for the Spring
boot interview but it requires time.

If you are short of time then there is no choice


but to go through the question as soon as
possible so that you can revise the essential
concepts and at least have some idea about it.

Reviewing this book will not only help you gain


both confidence and knowledge to answer the
questions but more importantly it will allow you to
drive the Java interview in your favor. This is the
single most important tip I can give you as a
Java

GROKKING THE SPRING BOOT 9


developer.

Always, remember, your answers drive


interviews, and these questions will empower
you to drive the Interviewer to your stronger
areas.

All the best for the Java interview and if you


have any questions or feedback you can always
contact me on Twitter at javinpaul or comment
on my blogs Javarevisited and Java67.

GROKKING THE SPRING BOOT 1


Topics

GROKKING THE SPRING BOOT 1


These are the topics which you need to prepare
for Spring boot interviews:

1. Core spring
2. Container, Dependency Injections and IoC
3. Spring Aspect-Oriented Programming
4. Bean Life Cycle
5. Spring MVC
6. REST
7. Spring Cloud
8. Microservices
9. Spring Data JPA
10. Spring Boot Testing
11. Spring Security
12. Spring Boot Core Concepts
13. Spring Boot Actuator

These are also the topics which you need to


prepare for Spring Professional certification,
which means you can kill two birds in one
stone. I mean, by preparing for spring
professional certification you can also get
yourself ready for potential Spring Boot
interviews and vice-versa.

That’s actually the biggest benefit of any


certification, the recognition part is great but the
intangible benefit you get in terms of in-depth
knowledge is far more valued, as it helps to
clear potential Job interviews.

GROKKING THE SPRING BOOT 1


Spring Bean
Lifecycle

GROKKING THE SPRING BOOT 4


Now that we have seen some questions on
Spring core and basics, it’s time for Spring MVC
interview questions which is probably the most
important thing because of the popularity of
Spring as an MVC framework and standard for
developing Java web applications.

1. What is a bean in Spring Framework?


A bean is nothing but a Java class (POJO - Plain
Old Java Object) whose life-cycle is managed
by Spring framework. This means instances of
bean are created, maintained and destroyed
by Spring container.

2. What is the default scope of bean


in the Spring framework?
The default scope of a Spring bean is the
Singleton scope, and in the web application
default scope
of a spring bean in request scope. Singleton
bean means the same instance of a bean is
shared with all other beans, while request scope
means a bean is alive only for a request.

3.Do Spring singleton beans are


thread- safe?

No, Spring singleton beans are not thread-


safe. Singleton doesn’t mean bean would be
thread- safe.

4.What is the difference


between a singleton and
prototype bean?

GROKKING THE SPRING BOOT 5


6.What is Bean Factory, have you
used XMLBeanFactory?

Ans: BeanFactory is a factory Pattern which


is based on IOC design principles.it is used to
make a clear separation between application
configuration and dependency from actual
code.
The XmlBeanFactory is one of the implementations
of BeanFactory which we have used in our project.

The
org.springframework.beans.factory.xml.
XmlBeanFactory is used to create bean
instances defined in our XML file.

BeanFactory factory = new XmlBeanFactory(new FileInputStream(

Or

ClassPathResource resource = new ClassPathResource(“beans.xm

7. What is the difference between


ApplicationContext and
BeanFactory in Spring
framework?

This one is a very popular Spring interview


question and often asks in an entry-level
interview. ApplicationContext is the preferred way
of using spring because of the functionality
provided by it and the interviewer wanted to
check whether you are familiar with it or not.

GROKKING THE SPRING BOOT 5


ApplicationContext BeanFactory
Here we can have In this only one config
file or .xml file
more
than one config files
possible
Application contexts Don’t support.
can publish events
to beans that are
registered as listeners

Support It’s not


internationalization
(I18N) messages

Support application Doesn’t support.


life-cycle events,
and
validation.
Supports many Doesn’t support.
enterprise services
such as JNDI access,
EJB
integration, remoting

That’s all about the difference between


ApplicationContext and BeanFactory in Spring
Framework.

8.What does the @Bean annotation


do in Spring?

The @Bean is a method level annotation and is


used to define methods which return Spring Bean.
In other words, it tells Spring framework that
this method will return an object that should be
registered as a bean in the Spring application
context.

GROKKING THE SPRING BOOT 5


which is rendered by View and Controller to help
in request processing and routing.
Neither Model knows about View, nor View is
dependent upon Model, which means the same
model can be rendered by different views e.g.
JSP, FreeMarker or it can even be written as
JSON or XML in case of RESTful Web Services

2. Do you need spring-mvc.jar in


your classpath or is it part of
spring-core?

The spring-mvc.jar is not part of spring-core,


which means if you want to use the Spring MVC
framework in your Java project, you must
include spring-mvc.jar in your application’s
classpath. In Java web applications, spring-
mvc.jar is usually placed inside /WEB-INF/lib
folder.

3.What is the DispatcherServlet and


what is it used for?

GROKKING THE SPRING BOOT 8


The DispatcherServlet is an implementation
of Front Controller design pattern which
handles all incoming web requests to a Spring
MVC application. A Front Controller pattern
(see Enterprise application design pattern) is
a common pattern in web applications whose
job is to receive all requests and route it to
different components of the application for
actual processing.

In the case of Spring MVC, DispatcherServlet


routes web requests to Spring MVC controllers.

In Spring MVC, DispatcherServlet is used for


finding the correct Controller to process a
request, which it does with the help of handler
mapping like @ RequestMapping annotation.

It is also responsible for delegating logical view


names to ViewResolver and then sending the
rendered response to the client.

4.Is the DispatcherServlet


instantiated via an application
context?

No, DispatcherServlet is instantiated by


Servlet containers like Tomcat or Jetty. You
must define DispatcherServlet into the web.xml
file as shown below.

GROKKING THE SPRING BOOT 8


You can see that the load-on-startup tag is 1 which
means DispatcherServlet is instantiated when
you deploy Spring MVC application to Tomcat or
any other servlet container. During instantiation,
it looks for a file servlet-name-context.xml and
then initializes beans defined in this file.

5.What is the root application


context in Spring MVC? How is it
loaded?
In Spring MVC, the context loaded using
ContextLoaderListener is called the “root”
application context which belongs to the whole
application while the one initialized using
DispatcherServlet is actually specific to that
servlet.

Technically, Spring MVC allows multiple


DispatcherServlet in a Spring MVC web

GROKKING THE SPRING BOOT 8


2.How does auto-configuration
work? How does it know what to
configure?
As explained in the previous example, Spring
Boot auto-configuration checks classpath and
makes decisions like if a Thymeleaf is present on
the classpath then it can configure a
Thymelead template resolver, view resolver,
and a template engine automatically.

If Spring Data JPA is on the classpath then it


can automatically create repository
implementations from repository interfaces.
Similarly, if Spring Security is present on the
classpath then it can configure a very basic
web security setup.

It is seriously powerful as it takes more than


200 such decisions every time an application
starts up.

3.What are some common Spring


Boot annotations?
Some of the common Spring Boot annotations are:

1. @SpringBootApplication

This is the most common Spring Boot


annotation and you will find it probably in every
single Spring Boot application. Since Spring
Boot allows you to execute your Web
application without deploying into any web
server like Tomcat.

You can run them just like you can run the
main class in Java, this annotation is used to
annotate the main class of your Spring Boot
application. It

GROKKING THE SPRING BOOT 13


also enables the auto-configuration feature of
Spring Boot.

Here is an example of using the @


SpringBootApplication in Java:

package boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.
SpringBootApplication;
import org.springframework.web.bind.annotation.
RequestMapping;
import org.springframework.web.bind.annotation.
RestController;

@SpringBootApplication
public class SpringBootDemo {

public static void main(String args[]) {


SpringApplication.run(SpringBootDemo.class,
args);
}

@RestController
class HelloController{

@RequestMapping(“/”)
public String hello(){
return “Hello Spring Boot”;
}

This is the simplest example of a RESTful web


service you can write using Spring and Java.
You can run this like any Java application by
right-clicking on the source file and “Run as
Java application” in Eclipse. After that, the
embedded

GROKKING THE SPRING BOOT 13


Spring
Testing

GROKKING THE SPRING BOOT 1


Spring Boot provides a @SpringBootTest
annotation, which can be used as an alternative
to the standard spring-test
@ContextConfiguration annotation when you
need Spring Boot features.

Spring Boot also provides @MockBean annotation


that can be used to define a Mockito mock for
a bean inside our ApplicationContext, which
means the mock declared in test class (by
using @MockBean) will be injected as a bean
within the application.

5.How can you create a shared


application context in a JUnit
integration test?

You can create a shared application


context in a JUnit integration test by
implementing the
ApplicationContextAware interface as shown in
the following example:

@RunWith(SpringRunner.class) @ContextConfiguration(classes =
public class BookBorrowServiceJUnit4ContextTests implements A
//some code to test
}

6.When and where do you


use @ Transactional in
testing?

If you remember, the @Transactional annotation


defines the scope of a single database transaction

GROKKING THE SPRING BOOT 1


is the central interface in Spring’s transaction
infrastructure.

14. How can we enable Spring


Data JPA features?
Answer: To enable Spring data JPA features, first
we have to define a configuration class and then,
we can use @EnableJpaRepositoties
annotation with it. This annotation will enable
the features.

15. Differentiate between


findById() and getOne().

Answer: The findById() is available in


CrudRepository while getOne() is available in
JpaRepository. The findById() returns null if
record does not exist while the getOne() will
throw an exception called
EntityNotFoundException.

GROKKING THE SPRING BOOT 2


Spring Cloud

GROKKING THE SPRING BOOT 22


Today is the world of cloud computing and
most of the new Java development is
happening on Cloud like AWS, GCP, and Azure.
That’s why it’s very important for Java
developers to learn about cloud and
frameworks and libraries which support cloud-
native development in Java, like Spring Cloud
from Spring project.

Spring is a powerful framework and probably


the most popular Java framework. It is very
popular and used widely throughout the world.
Spring Cloud is a module provided by Spring
that is used to develop cloud-based allocations.
In simple words, Spring cloud provides Rapid
Application Development (RAD) features to
work with Spring.

Because of its popularity and combination of


Spring Boot and Spring Cloud for developing
cloud-native Java applications and
Microservices, you will find many Spring cloud-
related questions on Java developer interviews.

If you are preparing for a Java developer


interview and your job requires knowledge of
Spring Cloud and other Microservice
technologies then it makes sense to spend
some time learning Spring Cloud and getting
yourself familiar with common and frequently
asked questions.

Since Spring Cloud is a bit complicated


concept, especially for beginners. That is why
there is a demand for experienced Spring cloud
developers. But as mentioned, it is a
complicated concept and interviews related to
it have generally tough questions, hence its
better to prepare well for this

GROKKING THE SPRING BOOT 22


topic

In this chapter, I am going to share the top 15


questions related to Spring cloud for interviews.
You can use these questions to not only prepare
for the Spring Cloud interview but also to learn
and explore essential Spring cloud concepts.

1. Explain Spring cloud? or, What


is Spring Cloud?

Answer: Spring cloud is a set of tools that


can be used by developers to quickly build
some
common patterns in distributed systems such
as service discovery, configuration
management, intelligent routing, etc.

2. What are the common


features of Spring cloud?

Answer: Here is a list of some of the most


common features of the Spring Cloud framework,
which greatly help in developing cloud-native
apps and Microservices:

a) Service registration and discovery


b) Routing
c) Service to service calls
d) Distributed and versioned configuration
e) load balancing and circuit breakers

3.Explain load balancing? or What


is load balancing?

GROKKING THE SPRING BOOT 22


<intercept-url pattern=”/secure/admin/**” access=”hasRole(‘A
<intercept-url pattern=”/secure/user/**” access=”hasRole(‘AP
<intercept-url pattern=”/secure/**” access=”isAuthenticated

That’s all about some of the frequently


asked Spring Security Interview
Questions. These questions are not only
great for preparing for Spring job interviews
but also for preparing for Spring
Professional Certifications. These questions
are based upon essential spring security
concepts, and a good knowledge of
Spring security goes a long way in creating and
maintaining a secure Java web and enterprise
application.

GROKKING THE SPRING BOOT 2


Thank you
for
reading!
However this was
just a sample...
If you want to continue reading, you can
purchase the full book and all future updates
here:

https://gum.co/hrUXKY

GROKKING THE SPRING BOOT 2

You might also like