APPLICATION
JSON.
In a web environment, the properties for init parameters of the ServletConfig object.
In a web environment, the properties for init parameters of the ServletContext object.
JNDI attributes from java:comp/env.
Java system properties (System.getProperties()).
The operating system environment variables.
The properties with random.* using RandomValuePropertySource.
The configuration properties files either with the application.properties or application.yml. The application property configuration files have their
own order of precedence as follows:
o Profile-specific application properties outside your packaged JAR, that is, in the /config subdirectory of the directory from which the application is
run (application-{profile}.properties and YAML variants).
o Profile-specific configuration files such as application-{profile}.properties and its YAML variants outside your packaged JAR but in a directory
from which the application is run.
o Profile-specific configuration files such as application-{profile}.properties and its YAML variants inside the packaged JAR file but in a package
named config.
o Profile-specific configuration files such as application-{profile}.properties and its YAML variants but at the root of the classpath.
o Application configuration files such as application.properties and its YAML variants outside your packaged JAR but in the /config subdi-rectory of
the directory from which the application is run.
o Application configuration files such as application.properties and its YAML variants outside your packaged JAR but in a directory from which the
application is run.
o Application configuration files such as application.properties and its YAML variants inside your packaged JAR but in a package named config.
o Application configuration files such as application.properties and its YAML variants inside your packaged JAR but at the root of the class-path.
o The @PropertySource annotations on your @Configuration classes.
o Default properties (specified using SpringApplication.setDefaultProperties).
The preceding list of the property sources is according to the order of precedence. If you define any property from a property source higher in the
preceding list, Spring Boot will override the same property defined in the property source lower in the above list. Similarly, any property defined in a
profile-specific application-{profile}. properties will override the defined property value of the same properties in an application.properties file at the
same location as the application-{profile}.properties file.
Any properties you define in the application.properties file will be overridden by the value of the defined same properties in the
application.properties in the /config subdirectory. We have seen the order of precedence of the several property sources in the Spring Boot
application. In Spring Boot, we can define properties either in the .properties file or in the .yml file. By default, Spring Boot creates the application.
properties file in the src/main/resource directory of the Spring application.
Customizing the name of the application. properties file
Spring Boot does not force you to use the same property file created by default (application.properties). You can change its name as per your
choice but you to give this name to the Spring Boot application by setting the spring.config.name property to the new name of the file. Let’s see the
following code snippet:
package com.dineshonjava.prodos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProdosApplication {
public static void main(String[] args) {
System.setProperty(“spring.config.name”, “prodos-application”);
SpringApplication.run(ProdosApplication.class, args);
}
}
As you can see in the preceding code snippet, we are using the prodos-application. properties file instead of the default application.properties file.
Note : But the value of the spring.config.name property must be defined as prodos-application and not prodos-application.properties; if you use
prodos-application. properties, the file would get named as prodos-application.properties.properties.
Let’s move on to the next section to see how to define properties in our configuration files either using the .properties file or its YAML variant.
Application configuration using a properties file
In the previous sections, we discussed about the application configuration using a properties file. The SpringApplication class loads properties
from the application. properties file in the following locations and adds them to the Spring environment:
A /config subdirectory of the current directory
The current directory
A classpath /config package
The classpath root
You can also define the profile specific properties files in the same locations mentioned above. The properties defined in the profile-specific
application properties file have a higher priority to override the same properties defined in the normal application properties file. Let’s see how we
can define some properties in the application property file:
//define embedded server configurations
server.port=8080
server.address=192.168.11.21
server.session-timeout=1800
server.context-path=/prodos
server.servlet-path=/admin
In the application.properties configuration file, we defined the embedded server in the Spring application. Spring Boot also provides another
variant to configure a Spring application using a YAML file. Let’s see in the next section.
Application configuration using a YAML file
The YAML file is an alternative to the .properties file and Spring Boot supports YAML files using the SpringApplication class. YAML is not a markup
language; it is a configuration file used to define properties in the hierarchical configuration format. Spring Boot uses the SnakeYAML library to
parse the YAML file. This library is automatically added to your application classpath by spring-boot-starters.
Note: If both the application.properties and application.yml files are present side by side at the same level of precedence, properties in
application.yml will override the properties in application.properties .
Spring Boot supports YAML for properties with hierarchical configuration data. YAML organizes the properties in groups. Let’s see the following
example for configuring the embedded server.
Configuring an embedded server
You can configure an embedded server in your Spring application as shown in the following the YAML configuration file:
//define embedded server configurations in YAML
server:
port: 8080
address: 192.168.11.21
session-timeout: 1800
context-path: /prodos
servlet-path: /admin
In the preceding configuration, you can see both the application.properties and application.yml files. Spring Boot supports both the configuration
files. You can choose any one of them to configure the properties in the Spring application.
Configuring a data source
Let’s see some other configuration related to a data source. You can configure a data source in your Spring application as shown in the following
YAML configuration file:
//define data source configurations
spring:
datasource:
url: jdbc:mysql://localhost/prodos
username: prododb
password: prodopassword
driver-class-name: com.mysql.jdbc.Driver
The preceding configuration is for a data source in the YAML file. We will discuss more about it in the coming chapters.
Configuring Logging
By default, Spring Boot uses Logback ( http://logback.qos.ch ) for logging. And also, Spring Boot writes the logs to the console at an INFO level.
You can take full control over the logging configuration by creating a logback.xml file at the root of the classpath. Let’s see the following
logback.xml file:
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
The logback.xml configuration file provides full control over the default configuration file. If you do not add this file to the classpath of the
application (src/main/resources), then the default logging configuration file is more or less equivalent to the above configuration file. You can make
in logging configuration file by changing the logging levels, specifying logs of the output file and pattern of logs.
But you can also make most of the changes without adding an external logging configuration file (logback.xml) along with the Spring Boot
configuration either with the application.properties or application.yml file. Let’s set the logging levels as shown in the following YAML configuration
file:
//define logging configurations
logging:
level:
root: WARN
org:
springframework:
security: DEBUG
In the preceding application.yml file, we set the root logging level to WARN but the we set the Spring security logs level to the DEBUG level. Now
let’s configure the logging output.
By default, Spring Boot writes logs to the console, but you can also write these logs to the rotating file (prodos.log). You can set a file or path in the
application.yml file as shown in the following configuration:
//define logging configurations
logging:
path: /var/logs/prodos/
file: prodos.log
level:
root: WARN
org:
springframework:
security: DEBUG
By default, Spring Boot includes the following:
SLF4J : Logging facade
Logback : SLF4J implementation
Spring Boot also supports other several logging frameworks such as Java Util Logging, Log4J, and Log4J2. But, it is recommended that you stick
with the default logging framework in the Spring Boot application and also use the SLF4J abstraction in your application code. You can exclude the
default logging framework and use another logging framework Log4J by just adding a dependency, as follows:
org.springframework.boot
spring-boot-starter-websocket
ch.qos.logback
logback-classic
org.slf4j
slf4j-log4j12
You can see that we added the log4j12 dependency in Maven and excluded the default logging framework, that is, logback.
Further, let’s move on to see how a single YAML file works for multiple profiles configuration in the Spring application. Let’s see, in the following
section, how to define multiple profiles in a single YAML file.
Multi-profile YAML documents
As we have seen how to define profile-specific application properties files, so in case of the .properties file, you need to define number of files for
the number of profiles you want to create, but in the YAML configuration file, you can define multiple profiles related properties in a single YAML
file, which is an advantage of YAML over the properties configuration file.
Spring Boot provides a spring.profiles key to indicate when the document needs to be applied. Let’s see the following example of how to define
multiple profile-specific configurations in a single YAML file:
//define configurations profile specifically in a single YAML file
Do'stlaringiz bilan baham: |