










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
What are Spring Boot Starters? Spring Boot Startersare the set of convenient dependency descriptors which can be easily included in any level of application.
Typology: Study notes
1 / 18
This page cannot be seen from the preview
Don't miss anything!
Spring Boot Starters are the set of convenient dependency descriptors which can
be easily included in any level of application. These starters work as a
bootstrapping process for the Spring related technologies, we no longer need to
worry about the dependencies and they will be automatically managed by
Spring Boot Starters.
The starters contain a lot of the dependencies that you need to get a project up
and running quickly and with a consistent, supported a set of managed transitive
dependencies. To summarize, Spring Boot Starters are just JAR files used by
Spring Boot for auto-dependency.
Read Spring Boot Starters for more detail.
For a complete list, read Spring Boot Starters List
It takes a lot of configurations and boilerplate code create a simple Spring MVC
application without Spring Boot_._ Spring Boot Auto Configuration provides an
opinionated approach to bootstrap your application. Auto-Configuration will
attempt to automatically try to set up our application with default behaviour
based on the jars in the classpath.
For example, if Spring Boot finds HSQLDB in our classpath, it will automatically
configure an in-memory database for us. Think of the auto-configuration as an
intelligent system which can provide ready to use the application to us based on
the configured jars in our classpath. For detail information please read our
article Spring Boot Auto Configuration
Spring Boot Initializr provides a simple interface to quickly bootstrap a Spring
Boot application. Here are some of the benefits or advantages of using Initilizr.
● Spring Initializr provides an extensible API to generate quick start projects. ● Reduce time to create an application setup. Application setup can be created using a few clicks. ● It increases Productivity ● Initializr offers a configuration structure to define all the aspects related to the project to generate: list of dependencies, supported java and boot versions.
This is achievable by Spring Boot Dev Tools module. It’s a powerful tool for
development. It helps developers to shorten the development cycle and enable
easy deployment and testing during development.
To enable this feature, add the following dependency to the Maven POM file.
Read Spring Boot Dev Tools for different features of Dev Tools.
Spring Boot includes support for the following embedded containers
Use the right “Starter” to configure the embedded container.
The actuator provides production-ready features for Spring Boot application. It
will help us to check and manage our application in the production environment.
We don’t need any code to get these features since they are available once the
actuator dependency is in the class-path. The actuator provides features like
auditing, health, metrics, environment information, thread dump etc. using
HTTP endpoints. Read Spring Boot Actuator for more detail.
Use the application.properties file to configure a custom port for Spring Boot
application. To change the server port, use server.port property.
server.port= 9001
Read Spring Boot Web Application Configuration for more detail.
To create a custom endpoint using Spring Boot 1.x, we should expose the
instance of the custom endpoint class as a bean. We need to implement
Endpoint
@Component public class CustomEndpoint implements Endpoint { //methodimplimentation }
Spring Boot 2.x changed it completely by introducing @Endpoint annotation.
Spring Boot expose endpoints with @Endpoint, @WebEndpointor and
@WebEndpointExtension over HTTP using Jersey, Spring MVC, or Spring Web
Flux. Read Custom Endpoint in Spring Boot Actuator for more detail.
Spring Boot provides options to use all popular logging API using the relevant
starter, by default Spring Boot use Commons Logging for its internal logging. If
we are using Spring Boot Starters for our application, Logback will be used for
logging by default unless we want to use any other logging API. To use any other
logging API, we need to add the correct starter in our application. In case we like
to use Log4j2 for logging configuration, all you have to add the log4j2 starter in
your application (You may have to exclude Logback using pom.xml file).
Spring Boot provides an easy way to configure and set logging levels for your
application. We can use application.properties file to configure the desired
Logging level for our application by using ‘logging.level.*=LEVEL’. Here is an
example for the same. Read Spring Boot Logging for more detail.
logging.level.com.javadevjournal.rest=WARN
Use the spring-boot-starter-security starter to enable the Spring security
support in your Spring Boot application.
< dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-security</ artifactId > </ dependency >
The Spring Framework provides extensive support for working with SQL
databases, from direct JDBC access using JdbcTemplate to complete “object-
relational mapping” technologies such as Hibernate. To connect configure the
database for your Spring Boot application, use the spring-boot-starter-jdbc or
spring-boot-starter-data-jpa starters. For datasource configuration, use the
application.properties file in your application.
spring.datasource.url = jdbc:mysql://localhost/javadevjournal spring.datasource.username = root spring.datasource.password = spring.datasource.driver-class-name = com.mysql.jdbc.Driver
Above example is to configure MySQL in your application. For more information
read Configuring MySQL for Spring Boot Application
Spring Boot Maven plugin provides Spring Boot support in maven. This plugin
provides options to create an executable jar or war files. Here are some of the
goals for this plugin.
● boot: run runs your Spring Boot application. ● spring-boot:repackage repackages your jar/war to be executable. ● spring-boot:start and spring-boot:stop to manage the lifecycle of your Spring Boot application (i.e. for integration tests). ● spring-boot:build-info generates build information that can be used by the Actuator.
To include this plugin in your project, add XML in the plugins section of your
pom.xml
< plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > < version >2.0.5.RELEASE</ version > < executions > < execution > < goals > < goal >repackage</ goal > </ goals > </ execution > </ executions > </ plugin > </ plugins >
To exclude specific auto-configuration classes, use the exclude attribute of
@EnableAutoConfiguration to disable them. Here is a sample code for the same.
@Configuration @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration. class }) public class CustomConfiguration { }
YAML is a superset of JSON.Spring Boot YAML as an alternative to the
application.properties file to define your project properties. The
SpringApplication class automatically supports YAML as an alternative to
properties whenever you have the SnakeYAML library on your classpath.
Let’s take the following example of the application.properties file.
environments.dev.url = https://dev.javadevjournal.com environments.dev.name = Developer Setup
This can be represented in the YAML files as follows.
environments: dev: url: 'https://dev.javadevjournal.com' name: 'Developer Setup'
This is one of the most important and core annotations from Spring Boot. We
use this annotation to mark the main class of our Spring Boot application.
@SpringBootApplication public class SpringOrderAnnotationApplication { public static void main(String[] args) { SpringApplication.run(SpringOrderAnnotationApplication. class , args); } }
@SpringBootApplication is a convenience annotation that is equal to
declaring @Configuration, @EnableAutoConfigurationand
@ComponentScan with their default attributes.
You have the option to use @Configuration,
@EnableAutoConfiguration, and @ComponentScan individually but the
recommendation is to @SpringBootApplication annotation. For more
detail, please read Spring Boot Annotations.
Spring Boot search specific location in the project for serving static contents. By
default, Spring Boot serves static content from a directory called /static (or
/public or /resources or /META-INF/resources) in the classpath or
from the root of the ServletContext.
We can put our custom static content in any of the above folders. For example,
put the custom.js file under /resources/static/custom.js. To refer to
this file in the view, simply use the following code
Spring Boot provides multiple ways to active profile. We can pass profile
information through the command line or use application.properties,
Spring Boot also provide a way to set profile programmatically.
Use profile specific configuration files in out Spring Boot application. We need
to the naming convention of application-{profile}.properties where
the profile defines the name of the intended profile. Profile specific files will be
loaded from the same location as application.properties file. Read
Introduction to Spring Profiles Using Spring Boot for more detail.
We can control the package type generation in our Spring Boot project using
spring-boot-maven-plugin to build a war file, we need to follow these 2
steps.
Here is a snapshot from pom.xml