Spring Boot

Spring VS Spring Boot

Spring Boot is based on spring framework. spring boot provides mainly starters for different dependencies. You need to put the starters into pom.xml file and it will download all the required libraries along with compatible versions. You only need to focus on your coding parts or business logic parts.

You have auto configurations in spring boot but in spring you have to configure such things such as datasource configuration.

Spring boot comes with embedded Tomcat server but for spring you have to use external server. You don’t need to deploy the app into external server for spring boot but for spring you have to deploy into external server.

Spring boot provides actuators that help you to monitor health of the app.

What are the components available in spring boot framework?

Components in Spring Boot are starter, actuator, auto-configuration, etc.

How to change port of server in Spring Boot?

By default, the embedded server start on port 8080.

In classpath of Spring boot application you need to add application.properties/yml file and you need to add the following line in this properties file:

server.port=<port number>

The <port number> should be replaced by 8000 or 9090 or whatever you wish.

What are the different spring bean scopes?

  • Singleton: The bean instance will be only once and same instance will be returned by the IOC container. It is the default scope.
  • Prototype: The bean instance will be created each time when requested.
  • Request: The bean instance will be created per HTTP request.
  • Session: The bean instance will be created per HTTP session.
  • Globalsession: The bean instance will be created per HTTP global session. It can be used in portlet context only.

How to configure database using Spring boot?

Out of the box, Spring Boot is very easy to use with the H2 Database. If the H2 database is found on your classpath, Spring Boot will automatically set up an in memory H2 database for your use.

But, Spring Boot can be configured to work on any type of database; for example: MySQL.

First you need to add the MySQL database drivers to our project. You will need to add the following dependency to your Maven POM file (pom.xml).

< dependency >
	< groupId > mysql< / groupId >
	< artifactId >mysql-connector-java<  /artifactId >
< / dependency >

You need to override the H2 database properties being set by default in Spring Boot. The nice part is, Spring Boot sets default database properties only when you don’t. So, when you configure MySQL for use. Spring Boot wont setup the H2 database anymore.

The following properties are need to configure MySQL with Spring Boot. You can see these are pretty standard Java data source properties. Since in my example project, I’m using JPA too, we need to configure Hibernate for MySQL too.

In Spring boot’s application.properties file:

spring.datasource.url= jdbc:mysql://localhost:3306/springbootdb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=create-drop

What is a Spring Boot starter POM? Why is it useful?

Those dependencies are meant to provide a unified entry to an ad-hoc skeleton project with all needed dependencies.

They should usually be inherited from your project descriptor (pom.xml) so that you get all parent dependecies with configured versions. No more burden to be done on the developer side:

Starter POMs are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project, and you are good to go.

How would you reload your changes on Spring Boot without having to restart server?

Include following maven dependency in the application.

< dependencies >
  < dependency >
   < groupId >org.springframework.boot< / groupId >
   < artifactId >spring-boot-devtools< / artifactId >
  < / dependency >
< / dependencies >

For Automatic restart:

Applications that use spring-boot-devtools will automatically restart whenever files on the classpath change. This can be a useful feature when working in an IDE as it gives a very fast feedback loop for code changes. By default, any entry on the classpath that points to a folder will be monitored for changes.

< dependency >
  < groupId >org.springframework.boot< / groupId >
  < artifactId >spring-boot-devtools< / artifactId >
  < optional >true< / optional >
< / dependency >

This can be achieved using DEV Tools. With this dependency any changes you save, the embedded tomcat will restart. Spring Boot has a Developer tools (DevTools) module which helps to improve the productivity of developers.

How would you 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.

The benefits of using Spring Transactions are:

  • 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

What are different ways to configure a class as Spring Bean?

There are three different ways to configure Spring Bean.

XML Configuration: This is the most popular configuration and we can use bean element in context file to configure a Spring Bean. For example:

< bean name="myBean" class="com.journaldev.spring.beans.MyBean">< /bean >

Java Based Configuration: If you are using only annotations, you can configure a Spring bean using @Bean annotation. This annotation is used with @Configuration classes to configure a spring bean. Sample configuration is:

@Configuration @ComponentScan(value="com.journaldev.spring.main")
public class MyConfiguration {
	@Bean public MyService getService(){ 
		return new MyService(); 
	}
}

To get this bean from spring context, we need to use following code snippet:

AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfiguration.class);

MyService service = ctx.getBean(MyService.class);

Annotation Based Configuration: We can also use @Component, @Service, @Repository and @Controller annotations with classes to configure them to be as spring bean. For these, we would need to provide base package location to scan for these classes. For example:

<context:component-scan base-package="com.journaldev.spring" />

How to get ServletContext and ServletConfig objects in a Spring Bean?

There are two ways to get Container specific objects in the spring bean.

Implementing Spring *Aware interfaces, for these ServletContextAware and ServletConfigAware interfaces.

Using @Autowired annotation with bean variable of type ServletContext and ServletConfig. They will work only in servlet container specific environment only though.

You can check example on how to get ServletConfig and ServletContext objects in Spring.