1.@ConditionalOnClass
作用:引入相关包才会构建类。
例:引入jackson的ObjectMapper才会构建JacksonConfig
@Configuration
@ConditionalOnClass(ObjectMapper.class)
@AutoConfigureBefore(JacksonAutoConfiguration.class)
public class JacksonConfig {
private static final String ASIA_SHANGHAI = "Asia/Shanghai";
@Bean
@ConditionalOnMissingBean
public Jackson2ObjectMapperBuilderCustomizer customizer() {
return builder -> {
builder.locale(Locale.CHINA);
builder.timeZone(TimeZone.getTimeZone(ASIA_SHANGHAI));
builder.simpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
builder.modules(new PigxJavaTimeModule());
};
}
}
2.@AutoConfigureBefore、@AutoConfigureAfter
作用:在***类之前/之后加载。是 spring-boot-autoconfigure 包下的注解
例:在AAAA之前加载,@AutoConfigureBefore(AAAA.class) 或 AutoConfigureBefore({AAAA.class, BBBB.class})
@AutoConfigureBefore(AAAA.class)
public class CCCC {
}
在AAAA之后加载,@AutoConfigureAfter(AAAA.class) 或 AutoConfigureAfter({AAAA.class, BBBB.class})
@AutoConfigureAfter(AAAA.class)
public class CCCC {
}