0% found this document useful (0 votes)
119 views

Spring Boot: Creating Spring Boot Projects With Eclipse and Maven

The document discusses Spring Boot, a rapid application development platform built on top of the Spring Framework. Spring Boot allows creating web applications packaged as JAR files with embedded web servers. It provides auto-configuration features and dependencies to get projects started quickly. The document also discusses creating Spring Boot projects with Eclipse and Maven, how Maven works to manage dependencies, and tools used with Spring Boot like Maven and IDEs.

Uploaded by

gayathri nath
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views

Spring Boot: Creating Spring Boot Projects With Eclipse and Maven

The document discusses Spring Boot, a rapid application development platform built on top of the Spring Framework. Spring Boot allows creating web applications packaged as JAR files with embedded web servers. It provides auto-configuration features and dependencies to get projects started quickly. The document also discusses creating Spring Boot projects with Eclipse and Maven, how Maven works to manage dependencies, and tools used with Spring Boot like Maven and IDEs.

Uploaded by

gayathri nath
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Spring Boot

Spring Boot is a rapid application development platform built on top of Spring


Framework. Spring Boot is a rapid application development platform built on top of
the popular Spring Framework. Spring Boot is mostly used to create web
applications but can also be used for command line applications. A Spring Boot
web application can be built to a stand-alone JAR. This JAR contains an
embedded web server that can be started with java -jar. Spring Boot provides
selected groups of auto configured features and dependencies, which makes it
faster to get started.

Creating Spring Boot Projects With Eclipse


and Maven
There are three options to create Spring Boot Projects with Eclipse and Maven

1. Spring Initializer
2. Use STS or STS Eclipse Plugin and create a Spring Boot Maven project directly
from Eclipse
3. Manually create a Maven project and add Spring Boot starter dependencies

How Does Maven Work?


A Maven repository contains all the JARs indexed by artifact ID and group ID. Once we
add a dependency to our pom.xml, Maven asks the Maven repository for the JAR
dependencies, giving group ID and the artifact ID as the input.

The JAR dependencies are stored on your machine in a folder called maven local
repository. All our projects refer to the JARs from here. A local repository is a temp
folder on your machine where Maven stores the JAR and dependency files that are
downloaded from the Maven repository.

Why Maven?
There is no need to store all the libraries in your project!

We just have to tell I need A, B, C and the tool would download the libraries and make
them available tous.

That’s Maven. The tool which we use to manage the libraries.

If a new version of the library is needed, we can change the version and our project is
ready!

Also, no need to worry about what libraries your library needs to work. For example,
Spring might need other libraries - logging, xml etc.

Once we declare a dependency on Spring, Maven would download

● Spring
● And all dependencies of Spring

Tools Used
● Maven 3.0+
● Any IDE (Here I used Eclipse for my implementation)
● JDK 1.7+
Spring Boot Runner
Application Runner

Application Runner is an interface used to execute the code after the Spring Boot
application started.

@Override

public void run(ApplicationArguments arg0) throws Exception {

System.out.println("Hello World Example from Application Runner");

This one will print the sysout line in the console as soon as the Spring boot application
has started.

Command Line Runner

Command Line Runner is an interface. It is used to execute the code after the Spring
Boot application started.

Application Properties
Application Properties support us to work in different environments. They are of two
types - Command Line Properties and Property File Properties.

Command Line Properties


Spring Boot application converts the command line properties into Spring Boot
Environment properties. Command line properties take precedence over the other
property sources. By default, Spring Boot uses the 8080 port number to start the
Tomcat.

Properties File
Properties files are used to keep N number of properties in a single file to run the
application in a different environment. In Spring Boot, properties are kept in the
application.properties file under the classpath.The application.properties file is located
in the src/main/resources directory.

YAML File
Spring Boot supports YAML based properties configurations to run the application.
Instead of application.properties, we can use application.yml file. This YAML file also
should be kept inside the classpath.

Use of @Value Annotation


The @Value annotation is used to read the environment or application property value
in Java code. The syntax to read the property value is :
@Value("${propertyname}")

Spring Boot Logging


Spring Boot uses Apache Commons logging for all internal logging. Spring Boot’s
default configurations provides a support for the use of Java Util Logging, Log4j2, and
Logback.

By default, all logs will print on the console window and not in the files. If we want to
print the logs in a file, then we have to set the property logging.file or logging.path in
the application.properties file.The log file path can be specified using the property

logging.path = /var/tmp/
Also we can specify the log file name using the property logging.file =
/var/tmp/mylog.log

Building RESTful Web Services


Spring Boot provides a very good support to building RESTful Web Services for enterprise
applications. This chapter will explain in detail about building RESTful web services using
Spring Boot.

Rest Controller
The @RestController annotation is used to define the RESTful web services. It serves
JSON, XML and custom response.
@RestController
public class ProductServiceController {
}
Request Mapping
The @RequestMapping annotation is used to define the Request URI to access the REST
Endpoints. We can define a Request method to consume and produce objects. The default
request method is GET.

@RequestMapping(value = "/products")
public ResponseEntity<Object> getProducts() { }

Request Body
The @RequestBody annotation is used to define the request body content type.

public ResponseEntity<Object> createProduct(@RequestBody Product product) {


}
Path Variable
The @PathVariable annotation is used to define the custom or dynamic request URI.
Request Parameter
The @RequestParam annotation is used to read the request parameters from the Request
URL. By default, it is a required parameter. We can also set default value for request
parameters.
public ResponseEntity<Object> getProduct(
@RequestParam(value = "name", required = false, defaultValue = "honey") String name) {
}

Interceptors
We use Interceptor in Spring Boot to perform operations under the following situations
● Before sending the request to the controller
● Before sending the response to the client

For example, we can use an interceptor to add the request header before sending the
request to the controller and add the response header before sending the response to
the client.
First we have to create @Component class that supports it and it should implement the
HandlerInterceptor interface.
The following are the three methods used while working on Interceptors
● preHandle() method − This is used to perform operations before sending the request to
the controller. This method should return true to return the response to the client.
● postHandle() method − This is used to perform operations before sending the response to
the client.
● afterCompletion() method − This is used to perform operations after completing the
request and response.

Internationalization
Internationalization is a process that makes the application adaptable to different
languages without making changes on the source code.Internationalization is a
readiness of Localization.
We need the following fro making Internalization in Spring Boot

1.Maven Dependencies
For development, we need the maven dependency:
2. LocaleResolver
In order for our application to be able to determine which locale is currently being used,
we need to add a LocaleResolver bean:
The LocaleResolver interface has implementations that determine the current locale
based on the session, cookies, the Accept-Language header, or a fixed value.

3. LocaleChangeInterceptor
We need to add an interceptor bean that will switch to a new locale based on the value
of the lang parameter appended to a request:

4. Defining the Message Sources


By default, a Spring Boot application will look for message files containing
internationalization keys and values in the src/main/resources folder.
The keys for the values that will be localized have to be the same in every file, with
values appropriate to the language they correspond to.If a key does not exist in a
certain requested locale, then the application will fall back to the default locale value.

Scheduling
Scheduling is a process of executing the tasks for the specific time period. Spring Boot
provides a good support to write a scheduler on the Spring applications.

The @EnableScheduling annotation is used to enable the scheduler for your application.
This annotation should be added into the main Spring Boot application class file.The
@Scheduled annotation is used to trigger the scheduler for a specific time period.

Fixed Rate scheduler is used to execute the tasks at the specific time. It does not wait for
the completion of the previous task. The values should be in milliseconds.
Fixed Delay scheduler is used to execute the tasks at a specific time. It should wait for the
previous task completion. The values should be in milliseconds.

Spring Boot Batch Service


Batch Service is a process to execute more than one command in a single task.To
create a Batch Service program, we need to add the Spring Boot Starter Batch
dependency and HSQLDB dependency in our build configuration file.
We need to add the @EnableBatchProcessing annotation in the configuration class
file. The @EnableBatchProcessing annotation is used to enable the batch operations
for your Spring Boot application.

Database Handling
Spring Boot provides very good support to create a DataSource for Database. We need not
write any extra code to create a DataSource in Spring Boot. Just adding the dependencies
and doing the configuration details is enough to create a DataSource and connect the
Database.We need to add the Spring Boot Starter JDBC dependency in our build
configuration file.To connect the MySQL database, we need to add the MySQL dependency
into our build configuration file.

To access the Relational Database by using JdbcTemplate in the Spring Boot application,
we need to add the Spring Boot Starter JDBC dependency in our build configuration file.
Then, if we @Autowired the JdbcTemplate class, Spring Boot automatically connects the
Database and sets the Datasource for the JdbcTemplate object.We can also keep ‘n’
number Datasources in a single Spring Boot application.

You might also like