Spring Boot AutoConfigure provides auto-configuration capabilities that automatically configure Spring applications based on jar dependencies present on the classpath
—
Spring Boot AutoConfigure provides additional configuration support for JMX management, logging, application info, cache management, and pre-initialization features.
import org.springframework.boot.autoconfigure.jmx.*;
import org.springframework.boot.autoconfigure.logging.*;
import org.springframework.boot.autoconfigure.info.*;
import org.springframework.boot.autoconfigure.cache.*;
import org.springframework.boot.autoconfigure.preinitialize.*;
import org.springframework.boot.autoconfigure.context.*;Configuration properties for JMX (Java Management Extensions) support.
@ConfigurationProperties("spring.jmx")
public class JmxProperties {
/**
* Whether to enable JMX support.
* Default is false.
*/
public boolean isEnabled();
public void setEnabled(boolean enabled);
/**
* Whether to use unique runtime object names.
* Default is false.
*/
public boolean isUniqueNames();
public void setUniqueNames(boolean uniqueNames);
/**
* MBeanServer bean name.
* Default is "mbeanServer".
*/
public String getServer();
public void setServer(String server);
/**
* Set the default JMX domain.
*/
public void setDefaultDomain(String defaultDomain);
/**
* Registration policy for MBeans.
*/
public RegistrationPolicy getRegistrationPolicy();
public void setRegistrationPolicy(RegistrationPolicy registrationPolicy);
/**
* Registration policy options.
*/
public enum RegistrationPolicy {
FAIL_ON_EXISTING,
IGNORE_EXISTING,
REPLACE_EXISTING
}
}Extension of MetadataNamingStrategy that supports a parent ApplicationContext for hierarchical JMX naming.
public class ParentAwareNamingStrategy extends MetadataNamingStrategy {
/**
* Set whether to ensure unique runtime object names.
*/
public void setEnsureUniqueRuntimeObjectNames(boolean ensureUniqueRuntimeObjectNames);
/**
* Set the application context.
*/
public void setApplicationContext(ApplicationContext applicationContext);
/**
* Get the JMX ObjectName for the given managed bean.
*/
public ObjectName getObjectName(Object managedBean, String beanKey)
throws MalformedObjectNameException;
}ApplicationContextInitializer that writes the ConditionEvaluationReport to the log.
public class ConditionEvaluationReportLoggingListener
implements ApplicationContextInitializer<ConfigurableApplicationContext>,
GenericApplicationListener {
/**
* Create a listener with custom log level.
*/
public static ConditionEvaluationReportLoggingListener forLogLevel(LogLevel logLevelForReport);
/**
* Initialize the application context.
*/
public void initialize(ConfigurableApplicationContext applicationContext);
/**
* Get the order of this listener.
*/
public int getOrder();
/**
* Check if this listener supports the given event type.
*/
public boolean supportsEventType(ResolvableType resolvableType);
/**
* Check if this listener supports the given source type.
*/
public boolean supportsSourceType(Class<?> sourceType);
/**
* Handle an application event.
*/
public void onApplicationEvent(ApplicationEvent event);
}A condition evaluation report message that can be logged or printed.
public class ConditionEvaluationReportMessage {
/**
* Create a new message for the given report.
*/
public ConditionEvaluationReportMessage(ConditionEvaluationReport report);
/**
* Create a new message for the given report with custom log level.
*/
public ConditionEvaluationReportMessage(ConditionEvaluationReport report, LogLevel logLevel);
/**
* Get the string representation of the report.
*/
public String toString();
}Configuration properties for project information including build and git metadata.
@ConfigurationProperties(prefix = "spring.info")
public class ProjectInfoProperties {
/**
* Get build information configuration.
*/
public Build getBuild();
/**
* Get git information configuration.
*/
public Git getGit();
/**
* Build information configuration.
*/
public static class Build {
/**
* Location of the build info properties file.
* Default is "classpath:META-INF/build-info.properties".
*/
public Resource getLocation();
public void setLocation(Resource location);
/**
* File encoding for build info.
*/
public Charset getEncoding();
public void setEncoding(Charset encoding);
}
/**
* Git information configuration.
*/
public static class Git {
/**
* Location of the git properties file.
* Default is "classpath:git.properties".
*/
public Resource getLocation();
public void setLocation(Resource location);
/**
* File encoding for git info.
*/
public Charset getEncoding();
public void setEncoding(Charset encoding);
}
}Configuration properties for lifecycle processing and shutdown behavior.
@ConfigurationProperties("spring.lifecycle")
public class LifecycleProperties {
/**
* Timeout for each shutdown phase.
* Default is 30 seconds.
*/
public Duration getTimeoutPerShutdownPhase();
public void setTimeoutPerShutdownPhase(Duration timeoutPerShutdownPhase);
}Configuration properties for message source internationalization.
@ConfigurationProperties("spring.messages")
public class MessageSourceProperties {
/**
* Get the comma-separated list of basenames.
* Default is "messages".
*/
public List<String> getBasename();
public void setBasename(List<String> basename);
/**
* Message bundles encoding.
* Default is UTF-8.
*/
public Charset getEncoding();
public void setEncoding(Charset encoding);
/**
* Get the cache duration for loaded resource bundles.
* Null means cache forever.
*/
public @Nullable Duration getCacheDuration();
/**
* Set the cache duration for loaded resource bundles.
* Null means cache forever.
*/
public void setCacheDuration(@Nullable Duration cacheDuration);
/**
* Whether to fall back to the system Locale if no files found for specific Locale.
* Default is true.
*/
public boolean isFallbackToSystemLocale();
public void setFallbackToSystemLocale(boolean fallbackToSystemLocale);
/**
* Whether to always apply the MessageFormat rules.
* Default is false.
*/
public boolean isAlwaysUseMessageFormat();
public void setAlwaysUseMessageFormat(boolean alwaysUseMessageFormat);
/**
* Whether to use the message code as the default message.
* Default is false.
*/
public boolean isUseCodeAsDefaultMessage();
public void setUseCodeAsDefaultMessage(boolean useCodeAsDefaultMessage);
/**
* Get the list of locale-independent property file resources containing common messages.
*/
public @Nullable List<Resource> getCommonMessages();
/**
* Set common messages shared across all locales.
*/
public void setCommonMessages(@Nullable List<Resource> commonMessages);
}Enum defining supported cache types in order of precedence.
public enum CacheType {
/**
* Generic caching using ConcurrentHashMap.
*/
GENERIC,
/**
* JCache (JSR-107) EHCache, Hazelcast, Infinispan, etc.
*/
JCACHE,
/**
* Hazelcast distributed cache.
*/
HAZELCAST,
/**
* Infinispan cache.
*/
INFINISPAN,
/**
* Couchbase cache.
*/
COUCHBASE,
/**
* Redis cache.
*/
REDIS,
/**
* Cache2k cache.
*/
CACHE2K,
/**
* Caffeine cache.
*/
CAFFEINE,
/**
* Simple cache using ConcurrentHashMap.
*/
SIMPLE,
/**
* No caching.
*/
NONE
}Interface for pre-initializing code in the background that may otherwise cause delays. Implementations should be registered in spring.factories.
public interface BackgroundPreinitializer {
/**
* Perform background pre-initialization.
* Called early in application startup to warm up expensive initializations.
*/
void preinitialize();
}spring:
jmx:
enabled: true
unique-names: true
server: mbeanServer
default-domain: com.example.app
registration-policy: replace_existingspring:
messages:
basename: messages,config.i18n.messages
encoding: UTF-8
cache-duration: 3600s
fallback-to-system-locale: true
always-use-message-format: false
use-code-as-default-message: falsespring:
info:
build:
location: classpath:META-INF/build-info.properties
encoding: UTF-8
git:
location: classpath:git.properties
encoding: UTF-8spring:
lifecycle:
timeout-per-shutdown-phase: 60simport org.springframework.boot.autoconfigure.jmx.JmxProperties;
import org.springframework.jmx.export.MBeanExporter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JmxConfiguration {
@Bean
public MBeanExporter mbeanExporter(JmxProperties jmxProperties) {
MBeanExporter exporter = new MBeanExporter();
exporter.setEnsureUniqueRuntimeObjectNames(jmxProperties.isUniqueNames());
exporter.setRegistrationPolicy(jmxProperties.getRegistrationPolicy());
return exporter;
}
}import org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener;
import org.springframework.boot.logging.LogLevel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class LoggingConfiguration {
@Bean
public ConditionEvaluationReportLoggingListener reportListener() {
return ConditionEvaluationReportLoggingListener.forLogLevel(LogLevel.DEBUG);
}
}import org.springframework.boot.autoconfigure.context.MessageSourceProperties;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MessageSourceConfiguration {
private final MessageSourceProperties properties;
public MessageSourceConfiguration(MessageSourceProperties properties) {
this.properties = properties;
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames(
properties.getBasename().toArray(new String[0])
);
messageSource.setDefaultEncoding(properties.getEncoding().name());
messageSource.setFallbackToSystemLocale(
properties.isFallbackToSystemLocale()
);
return messageSource;
}
}import org.springframework.boot.autoconfigure.preinitialize.BackgroundPreinitializer;
public class DatabaseConnectionPreinitializer implements BackgroundPreinitializer {
@Override
public void preinitialize() {
// Warm up database driver
try {
Class.forName("org.postgresql.Driver");
System.out.println("Database driver pre-initialized");
} catch (ClassNotFoundException e) {
// Handle error
}
}
}Register in META-INF/spring.factories:
org.springframework.boot.autoconfigure.preinitialize.BackgroundPreinitializer=\
com.example.DatabaseConnectionPreinitializerimport org.springframework.boot.autoconfigure.cache.CacheType;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableCaching
public class CacheConfiguration {
@Bean
public CacheManager cacheManager() {
// Choose cache implementation based on requirements
return new ConcurrentMapCacheManager("users", "products");
}
}