
By default, INFO logging messages are shown, including some relevant startup details, such as the user that launched the application.
Lazy Initialization
When lazy initialization is enabled, beans are created as they are needed rather than during application startup.
spring.main.lazy-initialization=true@Lazy(false) annotation:disable lazy initialization for certain beans
Customizing the Banner
adding a banner.txt file to your classpath or by setting the spring.banner.location property to the location of such a file. If the file has an encoding other than UTF-8, you can set spring.banner.charset.
SpringApplication.setBanner(…) method
spring.main.banner-mode property System.out(console)、logger (log)、not produced at all (off)
The printed banner is registered as a singleton bean under the following name: springBootBanner.
Customizing SpringApplication
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MyApplication.class);
application.setBannerMode(Banner.Mode.OFF);
application.run(args);
}
}The internal state of Spring Boot applications is mostly represented by the Spring ApplicationContext.
Internally, Spring Boot uses events to handle a variety of tasks. Application events are sent in the following order, as your application runs:
ApplicationStartingEvent is sent at the start of a run but before any processing, except for the registration of listeners and initializers.
ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known but before the context is created.
ApplicationContextInitializedEvent is sent when the ApplicationContext is prepared and ApplicationContextInitializers have been called but before any bean definitions are loaded.
ApplicationPreparedEvent is sent just before the refresh is started but after bean definitions have been loaded.
ApplicationStartedEvent is sent after the context has been refreshed but before any application and command-line runners have been called.
AvailabilityChangeEvent is sent right after with LivenessState.CORRECT to indicate that the application is considered as live.
ApplicationReadyEvent is sent after any application and command-line runners have been called.
AvailabilityChangeEvent is sent right after with ReadinessState.ACCEPTING_TRAFFIC to indicate that the application is ready to service requests.
ApplicationFailedEvent is sent if there is an exception on startup.
Accessing Application Arguments
import java.util.List;
import org.springframework.boot.ApplicationArguments;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
public MyBean(ApplicationArguments args) {
boolean debug = args.containsOption("debug");
List<String> files = args.getNonOptionArgs();
if (debug) {
System.out.println(files);
}
// if run with "--debug logfile.txt" prints ["logfile.txt"]
}
}If you need to run some specific code once the SpringApplication has started, you can implement the ApplicationRunner or CommandLineRunner interfaces.
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) {
// Do something...
}
}Application Exit
Each SpringApplication registers a shutdown hook with the JVM to ensure that the ApplicationContext closes gracefully on exit.
Sources are considered in the following order:
SpringApplication.setDefaultProperties).
@PropertySource annotations on your @Configuration classes. Please note that such property sources are not added to the Environment until the application context is being refreshed. This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.
application.properties files).
RandomValuePropertySource that has properties only in random.*.
System.getProperties()).
java:comp/env.
ServletContext init parameters.
ServletConfig init parameters.
SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
@DynamicPropertySource annotations in your tests.
@TestPropertySource annotations on your tests.
$HOME/.config/spring-boot directory when devtools is active.
Config data files are considered in the following order:
application.properties and YAML variants).
application-{profile}.properties and YAML variants).
application.properties and YAML variants).
application-{profile}.properties and YAML variants).
If you have configuration files with both .propertiesand YAML format in the same location, .properties takes precedence.
读取配置:
Property values can be injected directly into your beans by using the @Value annotation, accessed through Spring’s Environmentabstraction, or be bound to structured objects through @ConfigurationProperties.
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
@Value("${name}")
private String name;
// ...
}command line properties (that is, arguments starting with --, such as --server.port=9000) always take precedence over file-based property sources.
Spring Boot will automatically find and load application.properties and application.yaml files from the following locations when your application starts:
/config package
config/ subdirectory in the current directory
config/ subdirectory
Wildcard locations only work with external directories. You cannot use a wildcard in a classpath: location.
Profile Specific Files
application-{profile}. For example, if your application activates a profile named prod and uses YAML files, then both application.yaml and application-prod.yaml will be considered.
configtree:
etc/
config/
myapp/
username
passwordspring.config.import=optional:configtree:/etc/config/You can then access or inject myapp.username and myapp.password properties from the Environment in the usual way.
Property placeholders can also specify a default value using a : to separate the default value from the property name, for example ${name:default}.
app.name=MyApp
app.description=${app.name} is a Spring Boot application written by ${username:Unknown}Working With YAML
If you use “Starters”, SnakeYAML is automatically provided by spring-boot-starter.
YAML files cannot be loaded by using the
@PropertySourceor@TestPropertySourceannotations. So, in the case that you need to load values that way, you need to use a properties file.
The YamlPropertiesFactoryBeanloads YAML as Properties and the YamlMapFactoryBean loads YAML as a Map. YamlPropertySourceLoader class if you want to load YAML as a Spring PropertySource.
Configuring Random Values
RandomValuePropertySource
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number-less-than-ten=${random.int(10)}
my.number-in-range=${random.int[1024,65536]}Type-safe Configuration Properties
@ConfigurationProperties
Relaxed Binding
As an example, consider the following @ConfigurationProperties class:
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "my.main-project.person")
public class MyPersonProperties {
private String firstName;
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}With the preceding code, the following properties names can all be used:
my.main-project.person.first-name Kebab case, which is recommended for use in .properties and YAML files.
my.main-project.person.firstName Standard camel case syntax.
my.main-project.person.first_name Underscore notation, which is an alternative format for use in .properties and YAML files.
MY_MAINPROJECT_PERSON_FIRSTNAME Upper case format, which is recommended when using system environment variables.
We recommend that, when possible, properties are stored in lower-case kebab format, such as my.person.first-name=Rod.
make it be available only in certain environments
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration(proxyBeanMethods = false)
@Profile("production")
public class ProductionConfiguration {
// ...
}By default, if you use the “Starters”, Logback is used for logging.
Log Format
2023-06-22T12:08:05.861Z INFO 22768 --- [ main] o.s.b.d.f.s.MyApplication : Starting MyApplication using Java 17.0.7 with PID 22768 (/opt/apps/myapp.jar started by myuser in /opt/apps/)
2023-06-22T12:08:05.872Z INFO 22768 --- [ main] o.s.b.d.f.s.MyApplication : No active profile set, falling back to 1 default profile: "default"
2023-06-22T12:08:09.854Z INFO 22768 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-06-22T12:08:09.892Z INFO 22768 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-06-22T12:08:09.892Z INFO 22768 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.10]
2023-06-22T12:08:10.160Z INFO 22768 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-06-22T12:08:10.162Z INFO 22768 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 4038 ms
2023-06-22T12:08:11.512Z INFO 22768 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-06-22T12:08:11.534Z INFO 22768 --- [ main] o.s.b.d.f.s.MyApplication : Started MyApplication in 7.251 seconds (process running for 8.584)ERROR, WARN, INFO, DEBUG, or TRACE.
--- separator to distinguish the start of actual log messages.
spring.messages.basename=messages,config.i18n.messages
spring.messages.fallback-to-system-locale=falseJackson is the preferred and default library. Auto-configuration for Jackson is provided and Jackson is part of spring-boot-starter-json.
@JsonComponent Custom Serializers and Deserializers
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.boot.jackson.JsonComponent;
@JsonComponent
public class MyJsonComponent {
public static class Serializer extends JsonSerializer<MyObject> {
@Override
public void serialize(MyObject value, JsonGenerator jgen, SerializerProvider serializers) throws IOException {
jgen.writeStartObject();
jgen.writeStringField("name", value.getName());
jgen.writeNumberField("age", value.getAge());
jgen.writeEndObject();
}
}
public static class Deserializer extends JsonDeserializer<MyObject> {
@Override
public MyObject deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException {
ObjectCodec codec = jsonParser.getCodec();
JsonNode tree = codec.readTree(jsonParser);
String name = tree.get("name").textValue();
int age = tree.get("age").intValue();
return new MyObject(name, age);
}
}
}In the absence of an Executor bean in the context, Spring Boot auto-configures a ThreadPoolTaskExecutor with sensible defaults that can be automatically associated to asynchronous task execution (@EnableAsync) and Spring MVC asynchronous request processing.
A ThreadPoolTaskScheduler can also be auto-configured if need to be associated to scheduled task execution (using @EnableScheduling for instance).
Most developers use the spring-boot-starter-test “Starter”, which imports both Spring Boot test modules as well as JUnit Jupiter, AssertJ, Hamcrest, and a number of other useful libraries.
One of the major advantages of dependency injection is that it should make your code easier to unit test. You can instantiate objects by using the new operator without even involving Spring. You can also use mock objects instead of real dependencies.
By default, @SpringBootTest will not start a server. You can use the webEnvironment attribute of @SpringBootTest to further refine how your tests run:
MOCK(Default) : Loads a web ApplicationContext and provides a mock web environment. Embedded servers are not started when using this annotation. If a web environment is not available on your classpath, this mode transparently falls back to creating a regular non-web ApplicationContext. It can be used in conjunction with @AutoConfigureMockMvc or @AutoConfigureWebTestClient for mock-based testing of your web application.
RANDOM_PORT: Loads a WebServerApplicationContext and provides a real web environment. Embedded servers are started and listen on a random port.
DEFINED_PORT: Loads a WebServerApplicationContext and provides a real web environment. Embedded servers are started and listen on a defined port (from your application.properties) or on the default port of 8080.
NONE: Loads an ApplicationContext by using SpringApplication but does not provide any web environment (mock or otherwise).
A compose.yml file is typically created next to your application which defines and configures service containers.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-docker-compose</artifactId>
<optional>true</optional>
</dependency>
</dependencies>Classes that implement auto-configuration are annotated with @AutoConfiguration.
spring.ssl.bundle.jks.mybundle.key.alias=application
spring.ssl.bundle.jks.mybundle.keystore.location=classpath:application.p12
spring.ssl.bundle.jks.mybundle.keystore.password=secret
spring.ssl.bundle.jks.mybundle.keystore.type=PKCS12参考资料: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features