Things done by Spring Boot
Tags: spring, springboot
Published: 2024-01-31 19:24:50

## Notice

I wrote this article and was originally published on [Qiita](https://qiita.com/saladlam/items/850a4a91706ed6cb1713) on 4 September 2022.

---

Spring Boot is a great tool for helping to build stand-alone, production-grade Spring based Applications. But most of the users do not know what Spring Boot is done for you. So I will explain this here.

# Pre-select library dependency and software

In the past without Spring Boot, for example if you wanted to use Spring MVC with Spring Security, you needed to include the following library JAR files into your project by dependency management tool.

- org.springframework.security:spring-security-config
- org.springframework.security:spring-security-core
- org.springframework.security:spring-security-crypto
- org.springframework.security:spring-security-web
- org.springframework:spring-aop
- org.springframework:spring-beans
- org.springframework:spring-context
- org.springframework:spring-core
- org.springframework:spring-expression
- org.springframework:spring-jcl
- org.springframework:spring-web
- org.springframework:spring-webmvc
- org.thymeleaf.extras:thymeleaf-extras-java8time
- org.thymeleaf.extras:thymeleaf-extras-springsecurity5
- org.thymeleaf:thymeleaf
- org.thymeleaf:thymeleaf-spring5

Your project cannot been built unless you provide ALL necessary library JAR files. Furthermore, compatibility issues exist between using particular version sets of library JAR files. You need to sort it out one by one if it exists.

In the world of Spring Boot, thanks to the existence of Spring Boot maven/gradle plugin. You don't need to handle this messy stuff again. To do the same task, just

1. Using Spring Boot maven/gradle plugin
2. Specify using 4 feature: spring-boot-starter-web, spring-boot-starter-thymeleaf, spring-boot-starter-security, thymeleaf-extras-springsecurity5

# Creates essentiel beans

In the past without Spring Boot, for example if you wanted to use JPA, you needed to define the following beans and configured them in your Spring Framework project.

- instance implements javax.sql.DataSource interface
- org.springframework.orm.jpa.JpaTransactionManager
- org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean

Below is an example of bean definition XML got from [here](https://github.com/Apress/pro-spring-14/blob/master/Chapter8/Chapter8/spring-data-jpa/src/main/resources/META-INF/spring/app-context-annotation.xml).

```xml
<?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"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jdbc
        http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <jdbc:embedded-database id="dataSource" type="H2">
        <jdbc:script location="classpath:META-INF/sql/schema.sql"/>
        <jdbc:script location="classpath:META-INF/sql/test-data.sql"/>
    </jdbc:embedded-database>

    <bean id="transactionManager"
        class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="packagesToScan"
             value="com.apress.prospring4.ch8"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.H2Dialect
                </prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="hibernate.jdbc.fetch_size">50</prop>
                <prop key="hibernate.jdbc.batch_size">10</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <context:annotation-config/>

    <context:component-scan base-package="com.apress.prospring4.ch8"/>

    <jpa:repositories base-package="com.apress.prospring4.ch8"
                      entity-manager-factory-ref="emf"
                      transaction-manager-ref="transactionManager"/>
</beans>
```

In order to compose this file correctly, basic knowledge of components is necessary. This is a great difficulty for programmers new to Spring Framework.

In the world of Spring Boot, just following two things need to do:
- configure database connection information (or create database bean and initialize SQL if you are using H2)
- include "spring-boot-starter-data-jpa" package into your project

That's all! Spring Boot will help you to create necessary beans and configure them.

# Packs all you need into one JAR file and provide single point of execution

In the past, for example to develop a traditional command line application using Spring Framework, just like other traditional application, perform the following

1. develop your code and pack it into a JAR file
1. collect required library JAR files and copy to "lib" folder
1. run the program by

```
java -cp <your project>.jar:lib/* your.project.class.Main
```

In the world of Spring Boot, Spring Boot maven/gradle plugin will instruct maven/gradle to pack your project class files and all dependencies into ONE JAR file. And then a small starter program (*org.springframework.boot.loader.JarLauncher*) is also included. In order to start, just type

```
java -jar <your project>.jar
```

Packing all the things you need into one file is particularly useful when deploying the project into the cloud environment.

# Log and environmental variables management

In the past, you need to define following entry in your bean definition XML.

```xml
<context:property-placeholder location="classpath:your_project.properties"/>
```

In the world of Spring Boot, all configuration (boths Spring Framework and your application related) is on **application.properties**. And localize and format messages string is on **messages.properties**.