@Configuration
public class MailNotifyConfiguration {
@Configuration
@ConditionalOnProperty("mail.server-url")
@AutoConfigureBefore({AdminServerNotifierAutoConfiguration.NotifierTriggerConfiguration.class, AdminServerNotifierAutoConfiguration.CompositeNotifierConfiguration.class})
public static class MailNotifierConfiguration {
private final ApplicationContext applicationContext;
public MailNotifierConfiguration(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Bean
@ConditionalOnMissingBean
@ConfigurationProperties("notify.mail")
public MailServerNotifier mailNotifier(InstanceRepository repository) {
return new MailServerNotifier(emailSender(), repository, mailNotifierTemplateEngine());
}
@Bean
@ConfigurationProperties("mail")
public EmailSender emailSender() {
return new EmailSender();
}
@Bean
public TemplateEngine mailNotifierTemplateEngine() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(this.applicationContext);
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(resolver);
return templateEngine;
}
}
}
public class EmailSender{
private String serverUrl;
public String getServerUrl() {
return serverUrl;
}
public void setServerUrl(String serverUrl) {
this.serverUrl = serverUrl;
}
//省略代码...
}
# 对应的配置文件
mail.server-url=http://server.com/mail/sendMail
notify.mail.enabled=true
notify.mail.to=exapmle@example.com
notify.mail.cc=exapmle@example.com
notify.mail.ignore-changes=
notify.mail.additional-properties.kbi_env=default
Spring Boot jar包含元数据文件,提供所有支持的配置属性的详细信息。这些文件旨在允许IDE开发人员在用户使用application.properties 或application.yml文件时提供上下文帮助和“代码完成” 元数据文件位于 jar包中的 META-INF/spring-configuration-metadata.json
You can easily generate your own configuration metadata file from items annotated with @ConfigurationProperties by using the spring-boot-configuration-processor jar. The jar includes a Java annotation processor which is invoked as your project is compiled. To use the processor, include a dependency on spring-boot-configuration-processor. https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor