, ApplicationListener, startupup spring -, . : -
a) spring.factories , .. src\main\resources\META-INF\spring. -
org.springframework.context.ApplicationListener=demo.CustomConfigListener
b) , CustomConfigListener
package demo;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.PropertySource;
public class CustomConfigListener implements ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
for(PropertySource<?> source : ((ApplicationEnvironmentPreparedEvent) event).getEnvironment().getPropertySources()){
if(source.getName().equals("applicationConfigurationProperties")){
if (source instanceof EnumerablePropertySource) {
for(String name : ((EnumerablePropertySource) source).getPropertyNames()){
System.out.println(name+" :: "+ ((EnumerablePropertySource) source).getProperty(name));
}
}
}
}
}
}
}
c) ConfigurationProperties
package demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(ignoreUnknownFields = false, prefix = "mail")
public class MailProperties {
private String host;
private int port;
private Smtp smtp;
public static class Smtp {
private boolean auth;
private boolean starttlsEnable;
}
}
d) , application.properties
mail.host=localhost
mail.port=25
mail.smtp.auth=false
mail.smtp.starttls-enable=false