1) What is Spring Boot?
A container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up. I.e., Applying IoC, objects are given their.
Spring Boot is a Spring module which provides RAD (Rapid Application Development) feature to Spring framework.
It is used to create stand alone spring based application that you can just run because it needs very little spring configuration.
For more information click here.
2) What are the advantages of Spring Boot?
- Create stand-alone Spring applications that can be started using java -jar.
- Embed Tomcat, Jetty or Undertow directly. You don't need to deploy WAR files.
- It provides opinionated 'starter' POMs to simplify your Maven configuration.
- It automatically configure Spring whenever possible.
For more information click here.
3) What are the features of Spring Boot?
- Web Development
- SpringApplication
- Application events and listeners
- Admin features
For more information click here.
4) How to create Spring Boot application using Maven?
There are multiple approaches to create Spring Boot project. We can use any of the following approach to create application.
- Spring Maven Project
- Spring Starter Project Wizard
- Spring Initializr
- Spring Boot CLI
For more information click here.
5) How to create Spring Boot project using Spring Initializer?
It is a web tool which is provided by Spring on official site. You can create Spring Boot project by providing project details.
For more information click here.
6) How to create Spring Boot project using boot CLI?
It is a tool which you can download from the official site of Spring Framework. Here, we are explaining steps.
Download the CLI tool from official site and For more information click here.
7) How to create simple Spring Boot application?
To create an application. We are using STS (Spring Tool Suite) IDE and it includes the various steps that are explaining in steps.
For more information click here.
8) What are the Spring Boot Annotations?
The @RestController is a stereotype annotation. It adds @Controller and @ResponseBody annotations to the class. We need to import org.springframework.web.bind.annotation package in our file, in order to implement it.
For more information click here.
9) What is Spring Boot dependency management?
Spring Boot manages dependencies and configuration automatically. You don't need to specify version for any of that dependencies.
Spring Boot upgrades all dependencies automatically when you upgrade Spring Boot.
For more information click here.
10) What are the Spring Boot properties?
Spring Boot provides various properties which can be specified inside our project's application.properties file. These properties have default values and you can set that inside the properties file. Properties are used to set values like: server-port number, database connection configuration etc.
For more information click here.
11) What are the Spring Boot Starters?
Starters are a set of convenient dependency descriptors which we can include in our application.
Spring Boot provides built-in starters which makes development easier and rapid. For example, if we want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project.
For more information click here.
12) What is Spring Boot Actuator?
Spring Boot provides actuator to monitor and manage our application. Actuator is a tool which has HTTP endpoints. when application is pushed to production, you can choose to manage and monitor your application using HTTP endpoints.
For more information click here.
13) What is thymeleaf?
It is a server side Java template engine for web application. It's main goal is to bring elegant natural templates to your web application.
It can be integrate with Spring Framework and ideal for HTML5 Java web applications.
For more information click here.
14) How to use thymeleaf?
In order to use Thymeleaf we must add it into our pom.xml file like:
For more information click here.
![Answers Answers](/uploads/1/2/5/6/125651066/202142763.png)
15) How to connect Spring Boot to the database using JPA?
Spring Boot provides spring-boot-starter-data-jpa starter to connect Spring application with relational database efficiently. You can use it into project POM (Project Object Model) file.
For more information click here.
16) How to connect Spring Boot application to database using JDBC?
Spring Boot provides starter and libraries for connecting to our application with JDBC. Here, we are creating an application which connects with Mysql database. It includes the following steps to create and setup JDBC with Spring Boot.
For more information click here.
17) What is @RestController annotation in Spring Boot?
The @RestController is a stereotype annotation. It adds @Controller and @ResponseBody annotations to the class. We need to import org.springframework.web.bind.annotation package in our file, in order to implement it.
For more information click here.
18) What is @RequestMapping annotation in Spring Boot?
The @RequestMapping annotation is used to provide routing information. It tells to the Spring that any HTTP request should map to the corresponding method. We need to import org.springframework.web.annotation package in our file.
For more information click here.
19) How to create Spring Boot application using Spring Starter Project Wizard?
There is one more way to create Spring Boot project in STS (Spring Tool Suite). Creating project by using IDE is always a convenient way. Follow the following steps in order to create a Spring Boot Application by using this wizard.
For more information click here.
20) Spring Vs Spring Boot?
Spring is a web application framework based on Java. It provides tools and libraries to create a complete cutomized web application.
Wheras Spring Boot is a spring module which is used to create spring application project that can just run.
In this post, we will see Spring interview interview questions. If you are java/j2ee developer and have some experienced on Spring, you are going to face Spring interview questions.
Here I am sharing a list of important Spring interview questions.
Hot Spring interview questions
1. What is Spring framework?
Spring framework is an open source framework created to solve the complexity of enterprise application development. One of the chief advantages of the Spring framework is its layered architecture, which allows you to be selective about which of its components you use. Main module for Spring are Spring core,Spring AOP and Spring MVC.
2. What are main features of Spring frameworks?
- Lightweight:spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 1MB. And the processing overhead is also very negligible.
- The basic concept of the Dependency Injection or Inversion of Control is that, programmer do not need to create the objects, instead just describe how it should be created.
- Aspect oriented (AOP):Aspect oriented programming refers to the programming paradigm which isolates secondary or supporting functions from the main program’s business logic. AOP is a promising technology for separating crosscutting concerns, something usually hard to do in object-oriented programming. The application’s modularity is increased in that way and its maintenance becomes significantly easier.
- Spring contains and manages the life cycle and configuration of application objects.
- Spring comes with MVC web application framework, built on core Spring functionality. This framework is highly configurable via strategy interfaces, and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI.
- Spring framework provides a generic abstraction layer for transaction management. This allowing the developer to add the pluggable transaction managers, and making it easy to demarcate transactions without dealing with low-level issues.
- The JDBC abstraction layer of the Spring offers a meaningful exception hierarchy, which simplifies the error handling strategy. Integration with Hibernate, JDO, and iBATIS: Spring provides best Integration services with Hibernate, JDO and iBATIS
3. Explain main modules of Spring ?
- Spring AOP:
One of the key components of Spring is the AOP framework. AOP is used in Spring:- To provide declarative enterprise services, especially as a replacement for EJB declarative services. The most important such service is declarative transaction management, which builds on Spring’s transaction abstraction.
- To allow users to implement custom aspects, complementing their use of OOP with AOP
- Spring ORM:
The ORM package is related to the database access. It provides integration layers for popular object-relational mapping APIs, including JDO, Hibernate and iBatis. - Spring Web:
The Spring Web module is part of Spring?s web application development stack, which includes Spring MVC. - Spring DAO:
The DAO (Data Access Object) support in Spring is primarily for standardizing the data access work using the technologies like JDBC, Hibernate or JDO. - Spring Context:
This package builds on the beans package to add support for message sources and for the Observer design pattern, and the ability for application objects to obtain resources using a consistent API. - Spring Web MVC:
This is the Module which provides the MVC implementations for the web applications. - Spring Core:
The Core package is the most import component of the Spring Framework.
This component provides the Dependency Injection features. The BeanFactory provides a factory pattern which separates the dependencies like initialization, creation and access of the objects from your actual program logic.
Spring Framework Architecture |
4. What is dependency injection(IOC) in Spring?
The basic concept of the dependency injection (also known as Inversion of Control pattern) is that you do not create your objects but describe how they should be created. You don’t directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up.![Spring Spring](/uploads/1/2/5/6/125651066/481401527.jpg)
i.e., Applying IoC, objects are given their dependencies at creation time by some external entity that coordinates each object in the system. That is, dependencies are injected into objects. So, IoC means an inversion of responsibility with regard to how an object obtains references to collaborating objects.
5. What are ways to inject dependency in Spring?
There are two ways to do dependency injection in Spring.
- Dependency injection via constructor.
A normal POJO class managed by Spring IOC container are called Spring beans. It is core part of Spring application.
2 4 6 | <bean id='countryBean'class='org.arpit.java2blog.Country'> <propertyname='capital'ref='CapitalBean'/> |
7. How can you configure Spring in your application?
There are 3 ways to do it.
- XML based configuration
- Java based configuration
- Annotation based configuration.
8. What is Spring XML based configuration?
In Spring XML based configuration, you define all dependency in an XML file. You define all your beans with tag in XML file and all dependencies are read using this XML file.
for example :Sample ApplicationContext.xml file
2 4 6 8 10 12 14 16 | <!--?xml version='1.0'encoding='UTF-8'?--> <beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'xmlns:aop='http://www.springframework.org/schema/aop' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd'> <bean id='CountryBean'class='org.arpit.javapostsforlearning.Country'> <propertyname='capital'ref='CapitalBean'/> <bean id='CapitalBean'class='org.arpit.javapostsforlearning.Capital'> </bean> |
You can read this ApplicationContext.xml using:
2 | ApplicationContext appContext=newClassPathXmlApplicationContext('ApplicationContext.xml'); |
9. What is Spring java based configuration?
In Spring Java based configuration, you inject all dependencies using java class only. You can use @Configuaration and @Bean annotations to do it.
2 4 6 8 10 12 14 16 | packageorg.arpit.java2blog.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration publicCountry getCountry() returnnewCountry('India'); |
Above file is equivalent to below spring configuration xml
2 4 6 8 10 12 14 | <?xml version='1.0'encoding='UTF-8'?> <beans xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:context='http://www.springframework.org/schema/context' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context/spring-context-3.0.xsd'> <bean id='countryObj'class='org.arpit.java2blog.Country'> </bean> |
To get above bean to application context, you need to use below code
2 4 | ApplicationContext appContext=newAnnotationConfigApplicationContext(ApplicationConfiguration.class); Country countryObj=(Country)appContext.getBean('countryObj'); |
You can refer Spring java based configuration for complete example.
10. What is spring annotation based configuration?
You can do dependency injection via annotation also instead of XML configuration. You can define bean autowiring using annotations. You can use @Component,@Repository,@Service and @Controller annotation to configure bean in Spring application.
Annotations wiring is not turned on by default. You need to turn it on using :
Annotations wiring is not turned on by default. You need to turn it on using :
2 4 6 | <?xml version='1.0'encoding='UTF-8'?> <context:annotation-config/> |
Once you put above code, you can start using annotation on class , fields or methods.
11. What are different bean scopes in Spring?
There are 5 types of bean scopes supported in spring
- singleton – Scopes a single bean definition to a single object instance per Spring IoC container.
- prototype – Return a new bean instance each time when requested
- request – Return a single bean instance per HTTP request.
- session – Return a single bean instance per HTTP session.
- globalSession – Return a single bean instance per global HTTP session.
12. What is default scope of bean in Spring?
singleton is default scope of a bean in Spring. You have to explicitly change scope of a bean if you want different scope.This is one of most asked spring interiew questions.
13. What is ApplicationContext and what are its functions?
ApplicationContext is an central interface for providing configuration information to an application.
An ApplicationContext provides the following functionalities:
- Bean factory methods, inherited from ListableBeanFactory. This avoids the need for applications to use singletons.
- The ability to resolve messages, supporting internationalization. Inherited from the MessageSource interface.
- The ability to load file resources in a generic fashion. Inherited from the ResourceLoader interface.
- The ability to publish events. Implementations must provide a means of registering event listeners.
- Inheritance from a parent context. Definitions in a descendant context will always take priority. This means, for example, that a single parent context can be used by an entire web application, while each servlet has its own child context that is independent of that of any other servlet.
You can initialize collection using list and value tag as below:
2 4 6 8 10 | <bean id='CountryBean'class='org.arpit.java2blog.Country'> <list> <value>West Bengal</value> </list> </bean> |
15. What do you mean by bean autowiring in Spring?
In Spring framework, you can wire beans automatically with auto-wiring feature. To enable it, just define the “autowire” attribute in .The Spring container can autowire relationships between collaborating beans without using and elements which helps cut down on the amount of XML configuration
2 | <bean id='countryBean'class='org.arpit.java2blog.Country'autowire='byName'> |
16. What are different modes of autowiring supported by Spring?
There are following autowiring modes which can be used to instruct Spring container to use autowiring for dependency injection.
Default, no auto wiring, set it manually via “ref” attribute as we have done in dependency injection via settor method post.
Autowiring by property name. Spring container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file and it tries to match it with name of bean in xml configuration file.
Autowiring by property datatype. Spring container looks at the properties of the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the beans name in configuration file. If more than one such beans exists, a fatal exception is thrown.
byType mode in constructor argument.
Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType.
17. What is Spring AOP?
Aspect oriented Programming is programming paradigm which is analogous to object oriented programming. Key unit of object oriented programming is class, similarly key unit for AOP is Aspect. Aspect enable modularisation of concerns such as transaction management, it cut across multiple classes and types. It also refers as a crosscutting concerns.
18. What is Aspect, Advice, Join point and pointcut in Spring AOP?
Aspect: An Aspect is a class that implements concerns that cut across different classes such as logging. It is just a name.
Joint Point : It is a point in execution of program such as execution of method. In Spring AOP, a join point always represents a method execution.
Advice : Action taken by aspect at particular join point. For example: Before execution of getEmployeeName() method, put logging. So here, we are using before advice.
Pointcut : Pointcut is an expression that decides execution of advice at matched joint point. Spring uses the AspectJ pointcut expression language by default.
19. What is @Qualifier annotation in Spring?
You can have more than one bean of same type in your XML configuration but you want to autowire only one of them ,so @Qualifier removes confusion created by @Autowired by declaring exactly which bean is to autowired.
You can read about Spring @Qualifier annotation for more details.
You can read about Spring @Qualifier annotation for more details.
20. What is @Required annotation in Spring?
This annotation simply indicates that the affected bean property must be populated at configuration time: either through an explicit property value in a bean definition or through autowiring. The container will throw an exception if the affected bean property has not been populated; this allows for eager and explicit failure, avoiding NullPointerExceptions or the like later on.
Suppose you have very large application and you get NullPointerExceptions because required dependency has not been injected then it is very hard to find out what goes wrong.So this annotation helps us in debugging the code.
You can read about Spring @Required annotation for more details.
Suppose you have very large application and you get NullPointerExceptions because required dependency has not been injected then it is very hard to find out what goes wrong.So this annotation helps us in debugging the code.
You can read about Spring @Required annotation for more details.
21. How will you handle Circular dependencies in Spring?
refer Circular dependencies in Spring for more details.
22. What happens when you inject prototype bean into singleton bean? Can you explain the behavior?
refer injecting prototype bean into singleton bean in Spring for more details.
That’s all about Spring interview questions.
You may also like:
Join Our News Letter – Stay Updated
Subscribe to Awesome Java Content.
We respect your privacy and take protecting it seriously.