springboot2
Managing the Application Availability State
@Component
public class ReadinessStateExporter {
@EventListener
public void onStateChange(AvailabilityChangeEvent<ReadinessState> event) {
switch (event.getState()) {
case ACCEPTING_TRAFFIC:
// create file /tmp/healthy
break;
case REFUSING_TRAFFIC:
// remove file /tmp/healthy
break;
}
}
}
@Component
public class LocalCacheVerifier {
private final ApplicationEventPublisher eventPublisher;
public LocalCacheVerifier(ApplicationEventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}
public void checkLocalCache() {
try {
//...
}
catch (CacheCompletelyBrokenException ex) {
AvailabilityChangeEvent.publish(this.eventPublisher, ex,
LivenessState.BROKEN);
}
}
}
Application Events and Listeners
- ApplicationContext初始化之前不能注册监听器, 但可以使用SpringApplication.addListeners(…) , SpringApplicationBuilder.listeners(…)
Listener: 长任务可能不执行,使用CommandLineRunner
- ApplicationStartingEvent
- ApplicationEnvironmentPreparedEvent
- ApplicationContextInitializedEvent
ApplicationPreparedEvent
- WebServerInitializedEvent
- ServletWebServerInitializedEvent,ReactiveWebServerInitializedEvent
- A ContextRefreshedEvent
- ApplicationStartedEvent
- AvailabilityChangeEvent
- ApplicationReadyEvent
- AvailabilityChangeEvent
- ApplicationFailedEvent
Accessing Application Arguments
import org.springframework.boot.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
@Component
public class MyBean {
@Autowired
public MyBean(ApplicationArguments args) {
boolean debug = args.containsOption("debug");
List<String> files = args.getNonOptionArgs();
// if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]
}
}
Using the ApplicationRunner or CommandLineRunner
在SpringApplication.run()结束前运行
import org.springframework.boot.*; import org.springframework.stereotype.*; @Component public class MyBean implements CommandLineRunner { public void run(String... args) { // Do something... } }
Application Exit
@SpringBootApplication
public class ExitCodeApplication {
@Bean
public ExitCodeGenerator exitCodeGenerator() {
return () -> 42;
}
public static void main(String[] args) {
System.exit(SpringApplication.exit(SpringApplication.run(ExitCodeApplication.class,
args)));
}
}
Application Startup
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
app.setApplicationStartup(new BufferingApplicationStartup(2048));
app.run(args);
}
@Value
- Java properties files, YAML files, environment variables, and command-line arguments
$--$server.port=9000
- SpringApplication.setAddCommandLineProperties(false) 设置命令行参数后不生效
- SPRING_APPLICATION_JSON='{"acme":{"name":"test"}}' 环境变量
- java -Dspring.application.json='{"acme":{"name":"test"}}' -jar myapp.jar 系统变量
- java -jar myapp.jar --spring.application.json='{"acme":{"name":"test"}}' 命令行参数
默认配置文件位置
- classpath
- classpath:/config
- current directory
- current directory/config
- 环境变量:spring.config.name可以自定义配置文件名称
spring.config.location 配置文件位置 需要以"/"结尾
- spring.config.additional-location
java -jar myproject.jar --spring.config.name=myproject java -jar myproject.jar --spring.config.location=optional:classpath:/default.properties,optional:classpath:/ov erride.properties,optional:file:./custom-config/ # Use the prefix optional: if the locations are optional and you don’t mind if they don’t exist.
- optional:classpath:/
- optional:classpath:/config/
- optional:file:./
- optional:file:./config/
- optional:file:./config/*/
- optional:classpath:custom-config/
optional:file:./custom-config/
@Random Value
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]}
@ConstructorBinding
- 2.2之前的属性绑定需要setter方法,之后使用该注解可以使用构造方法绑定
松散绑定,不区分大小写,可已添加连字符
Map绑定
- Map<String,String>
acme.map.[/key1]=value1 acme.map.[/key2]=value2 acme.map./key3=value3
Logging
- logging.file.name
- logging.file.path
- logging.level.root=warn
- logging.level.org.springframework.web=debug
- logging.level.org.hibernate=error
- $logging.register-shutdown-hook=true$
国际化
spring.messages.basename=messages,config.i18n.messages
spring.messages.fallback-to-system-locale=false
响应式webflux
- spring-boot-starter-webflux
- 异步springmvc
MVC
- @EnableWebMvc
- DelegatingWebMvcConfiguration
自定义对象转json
import java.io.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import org.springframework.boot.jackson.*; @JsonComponent public class Example { public static class Serializer extends JsonSerializer<SomeObject> { // ... } public static class Deserializer extends JsonDeserializer<SomeObject> { // ... } }
static content
- /static ,/public, /resources, /META-INF/resources
- WebMvcConfigurer and overriding the addResourceHandlers
评论已关闭