Spring Boot: Creating Spring Boot Projects With Eclipse and Maven
Spring Boot: Creating 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
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.
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.
● 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
This one will print the sysout line in the console as soon as the Spring boot application
has started.
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.
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.
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
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.
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:
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.
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.