Spring Boot
Spring Boot is part of Spring Ecosystem that will help to manage the first steps to start an application. It handles low-level, predictable set-up for the developers and It has many non-functional features common to large numbers of projects.
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". Some Features: (1) Create stand-alone Spring applications (2) Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files) (3) Provide opinionated 'starter' dependencies to simplify your build configuration [1]
The Spring Boot manage compatible dependences, excluding dependencies will not be used or finding the right version. It solve JARs as spring-boot-*, spring-context-*, spring-core-* and spring-beans-*.
A good way to manage multiple dependencies is using spring-boot-starter. You don't need inform the version.
Starters are a set of convenient dependency descriptors that you can include in your application. The starters contain a lot of the dependencies that you need to get a project up and running quickly and with a consistent, supported set of managed transitive dependencies.[2]
As instance, if you need to use test libraries coordinate by Spring boot you should to use spring-boot-start-test. It will brings spring-test-*, junit-*, mockito-*, etc. Others examples of it are:
- spring-boot-start-jdbc
- spring-boot-start-data-jpa
- spring-boot-start-web
- spring-boot-start-batch
To run the application is necessary pom.xml (setup dependencies), application.properties (general configurations) and application class to launch the application. A simple way to create an initial app is going the the start.spring.io and downloading it with all dependencies you believe is necessary. You can do your "Hello World" reading this quickstart.
Developer Tool
The DevTools is a collection of tools included by Spring boot to make developer's life easier (spring-boot-devtools).
One feature is it has the cache disabled. Also, it allows the application be restart automatically when files on the classpath are changed.
If you prefer don't have the behaviour to restart the application you can disable it by the line bellow.
System.setProperty("spring.devtools.restart.enabled", "false");
In the same direction, there is the LiveReload server used to trigger a browser refresh when the resource is changed. To disable it you can change the property bellow.
spring.devtools.livereload.enabled
Annotation
@SpringBootApplication identify to Spring framework the main class to be handled by Spring. It allows app to have auto-configuration (@EnableAutoConfiguration), component scan (@ComponentScan) and be able to define extra configuration (@SpringBootConfiguration). Then, when you are using @SpringBootApplication you don't need use the other three annotations.
@EnableAutoConfiguration used to let Spring be responsible for the configuration based on the jar dependencies that you have added. You can combine @SpringBootApplication and @Configuration or @EnableAutoConfiguration and @Configuration, but not @SpringBootApplication and @EnableAutoConfiguration [3]
@ComponentScan enable @Component scan on the package where the application is located.[4]
@SpringBootConfiguration enable registration of extra beans in the context or the import of additional configuration classes.[4]
@Configuration is used to identify the class will define the configuration to the application. Even be possible configure using XML, Spring is Java-based configuration.
@Import is used to add other configuration classes. If you are using XML you should use @ImportResource.
@SpringBootTest: To integration test you can use @SpringBootTest(classes=Application.class) to load specified configuration. That annotation will searches for @SpringBootConfiguration class and creates application context for the test.
Spring Boot Features
- SpringApplication.run can be used to start the application inside the main method.
- It make possible the application start lazily. Which means the beans will be initialized by demand. It will reduce the time to start the application.
- It's possible to be customized.
- SpringApplicationBuilder will give support to use ApplicationContext hierarchy.
- It makes possible tracking the application startup sequence using ApplicationStartup.
Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments.
One example is using the annotation @Value that has the property value injected into the bean. That value can comes from application.properties which can has different values by environment (e.g. application-prod.properties). In the same direction is possible to use JavaBean properties by the use of @ConfigurationProperties and define properties to be used.
Spring Profiles provide a way to segregate parts of your application configuration and make it be available only in certain environments. Any @Component, @Configuration or @ConfigurationProperties can be marked with @Profile to limit when it is loaded
The internal logging use is done by the use of Commons Logging with default configurations for Java Util Logging, Log4J2, and Logback. If you are using the starters you will use Logback. It's possible to do all the configuration about the logs as color, text, export to a file, etc.
Spring Boot supports localized messages so that your application can cater to users of different language preferences. By default, Spring Boot looks for the presence of a messages resource bundle at the root of the classpath.
Spring Boot provides integration with three JSON mapping libraries: Gson, Jackson, JSON-B.
Spring Boot is Well prepared to web application development. It has embedded servers (e.g. Tomcat, Jetty). Inside the pom you can use spring-boot-starter-web or spring-boot-starter-webflux. Also, It provides auto-configuration for Spring MVC. However, if you are using REST then you can use one of the JAX-RS implementation that Spring support as Jersey instead the Spring MVC.
If Spring Security is on the classpath, then web applications are secured by default. Spring Boot relies on Spring Security’s content-negotiation strategy to determine whether to use httpBasic or formLogin. To add method-level security to a web application, you can also add @EnableGlobalMethodSecurity with your desired settings.
MVC Security: The default security configuration is implemented in SecurityAutoConfiguration and UserDetailsServiceAutoConfiguration.
WebFlux Security: Similar to Spring MVC applications, you can secure your WebFlux applications by adding the spring-boot-starter-security dependency. The default security configuration is implemented in ReactiveSecurityAutoConfiguration and UserDetailsServiceAutoConfiguration.
OAuth2: it is a widely used authorization framework that is supported by Spring. If you have spring-security-oauth2-client on your classpath, you can take advantage of some auto-configuration to set up an OAuth2/Open ID Connect clients.
Spring Boot provides a number of utilities and annotations to help when testing your application. Test support is provided by two modules: spring-boot-test contains core items, and spring-boot-test-autoconfigure supports auto-configuration for tests. Most developers use the spring-boot-starter-test “Starter”, which imports both Spring Boot test modules as well as JUnit Jupiter, AssertJ, Hamcrest, and a number of other useful libraries.
Auto-configuration
Spring boot has auto-configuration support to many resources as embedded Database Support (H2, HSQL, and Derby), JdbcTemplate, sending Email and Spring Session.
Spring Boot Actuator: Production-ready Features
Spring has many resources to help you to monitor your application. You can monitor the endpoints, HTTP, JMX, using logs, audit. If you need monitor your application click here to go deep.
Conclusion
Your project is not dependent on Spring Boot. You can use many parts of the framework in a separate way. However, Spring Boot gives productivity to the development.
O que é Spring Boot (Algaworks) | Reload |
---|---|
Spring Boot: the best and the worst | Spring Boot - How to Create and Run? |