Spring Boot aims to make it easy to create Spring-powered,
production-grade applications and services with minimum fuss. It takes an
opinionated view of the Spring platform so that new and existing users can
quickly get to the bits they need. You can use it to create stand-alone Java
applications that can be started using ‘java
-jar’ or more traditional WAR deployments. We also provide a
command line tool that runs ‘spring scripts’.
The
diagram below shows Spring Boot as a point of focus on the larger Spring
ecosystem. It presents a small surface area for users to approach and extract
value from the rest of Spring
How
to run spring boot without a web container?
Simply
start your spring boot app in a non-web environment:
new SpringApplicationBuilder()
.sources(SpringBootApp.class)
.web(false)
.run(args);
Also, you obviously should not add the spring-boot-starter-web dependency.
By default, spring boot launches a web
container if it finds one in the classpath. Using web(false)ensures that it does not happen. Tomcat
could be included by another dependency without your knowledge, so it's better
to disable the web environment if that is your goal.
What is Spring?
Simply put, the
Spring framework provides comprehensive infrastructure support for developing
Java applications.
It’s packed with
some nice features like Dependency Injection and out of the box modules like:
- Spring JDBC
- Spring MVC
- Spring
Security
- Spring AOP
- Spring ORM
- Spring Test
These modules can
drastically reduce the development time of an application.
For example, in
the early days of Java web development, we needed to write a lot of boilerplate
code to insert a record into a data source. But by using the JDBCTemplate of
the Spring JDBC module we can reduce it to a few lines of code with only a few
configurations.
What is Spring Boot?
Spring Boot is
basically an extension of the Spring framework which eliminated the boilerplate
configurations required for setting up a Spring application.
It takes an
opinionated view of the Spring platform which paved the way for a faster and
more efficient development eco-system.
Here are just a
few of the features in Spring Boot:
- Opinionated
‘starter’ dependencies to simplify build and application configuration
- Embedded
server to avoid complexity in application deployment
- Metrics,
Helth check, and externalized configuration
- Automatic
config for Spring functionality – whenever possible
Maven Dependencies
First of all, let’s look at the minimum dependencies
required to create a web application using Spring:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
Unlike
Spring, Spring Boot requires only one dependency to get a web application up
and running:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
Spring
Boot provides a number of starter dependencies for different Spring modules.Some of the most
commonly used ones are:
- spring-boot-starter-data-jpa
- spring-boot-starter-security
- spring-boot-starter-test
- spring-boot-starter-web
- spring-boot-starter-thymeleaf
Application Configuration with Spring
Boot:
Configuration for applications vary from
one environment to another
·
You would want to
connect to a different database or queues
·
You would want to
connect with different services
·
You would want to
configure less logging in production
·
You might want to
have different custom configuration
Application Configuration with Spring
Boot application.properties
Spring Boot allows you to configure
your application configuration using a file named application.properties
application.properties can reside
anywhere in the classpath of the application.
In application.properties, we can use
the “logging.level” prefix to set logging levels.
logging.level.some.package.path=DEBUG
logging.level.some.other.package.path=ERROR
You can configure a log file by using
logging.file property in application.properties. The logging here would be in
addition to the logging in console.
logging.file=\path_to\logfile.log
You can also configure the port to run
the server on using server.port
server.port = 9080
For complete list of options refer
spring documentation - https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
How application configuration will
point common location:
Spring Boot looks at Frameworks
available on the CLASSPATH b) Existing configuration for the application. Based
on these, Spring Boot provides basic configuration needed to configure the application
with these frameworks. This is called Auto Configuration.
For external configuration follow the
below link:
How to load external property files
into Spring Boot application?
It is a standard practice that during
our production deployments, our application loads property files from external
locations. This helps us to change our configurations without changing our
code. In this page, we will come to know how to load external property files
into Spring Boot application.
By default, Spring Boot look for
externalized default property file application.properties into given below
predetermined locations:
In the classpath root.
In the package "/config" in
classpath.
In the current directory.
In the "/config" directory
of current folder.
Now lets say, your application
requires externalized properties like application.properties and another
property file myapp.properties. The both properties can be in the same folder
or they can be in different folder. There are 3 ways of doing it.
Command line arguments
In the first approach, all you need to
do is pass folder names and property names as part of command line arguments as
shown below:
Terminal:
java -jar myapp.jar --spring.config.name=application,myapp
--spring.config.location=classpath:/data/myapp/config,classpath:/data/myapp/external/config
In the above command, we are passing
property file name as part of "--spring.config.name" variable and
folder location as part of "--spring.config.location" variable.
Environment variables
In the second approach, you can
configure your externalized configuration details into environment variables
and your Spring Boot application will read it from your environment as shown
below:
Terminal:
set
SPRING_CONFIG_NAME=application,myapp
set SPRING_CONFIG_LOCATION=classpath:/data/myapp/config,classpath:/data/myapp/external/config
java -jar myapp.jar
Programatically loding configurations:
ort org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import
org.springframework.boot.autoconfigure.SpringBootApplication;
import
org.springframework.boot.builder.SpringApplicationBuilder;
import
org.springframework.context.ConfigurableApplicationContext;
import
org.springframework.core.env.ConfigurableEnvironment;
@SpringBootApplication
public class SpringBootWebApplication
{
private static Logger logger =
LoggerFactory.getLogger(SpringBootWebApplication.class);
public static void main(String[] args)
throws Exception {
ConfigurableApplicationContext
applicationContext = new
SpringApplicationBuilder(SpringBootWebApplication.class).properties("spring.config.name:application,myapp", spring.config.location:classpath:/data/myapp/config,classpath:/data/myapp/external/config").build().run(args);
ConfigurableEnvironment environment =
applicationContext.getEnvironment();
logger.info(environment.getProperty("cmdb.resource-url"));
}
}
No comments:
Post a Comment