Understanding Spring Boot, Spring MVC, and Spring: Differences and Auto‑Configuration Mechanism
This article explains the distinctions between Spring, Spring MVC, and Spring Boot, illustrates how Spring Boot achieves automatic configuration through annotations like @SpringBootApplication, @EnableAutoConfiguration, and @ComponentScan, and provides detailed code examples of the underlying mechanisms and related configuration classes.
Spring is a family of frameworks built on the core concepts of IoC (dependency injection) and AOP (aspect‑oriented programming). Spring MVC is a lightweight web module that uses DispatcherServlet, ModelAndView, and ViewResolver to simplify URL routing, session handling, and view rendering. Spring Boot adds automatic configuration on top of Spring, reducing boilerplate setup by providing sensible defaults and out‑of‑the‑box integration for common libraries such as Jackson, JDBC, Mongo, Redis, and Mail.
The simplest way to start a Spring Boot application is a single class annotated with @SpringBootApplication and a main method that calls SpringApplication.run:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}The @SpringBootApplication annotation itself is a meta‑annotation that combines @SpringBootConfiguration (which is essentially @Configuration), @EnableAutoConfiguration, and @ComponentScan:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {
@AliasFor(annotation = EnableAutoConfiguration.class)
Class<?>[] exclude() default {};
@AliasFor(annotation = EnableAutoConfiguration.class)
String[] excludeName() default {};
} @Configurationmarks a class as a Java‑based configuration source, replacing traditional XML configuration. For example, a filter bean can be defined as:
@Configuration
public class DruidConfiguration {
@Bean
public FilterRegistrationBean statFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
} @ComponentScanautomatically discovers classes annotated with @Component, @Repository, @Service, etc., within the specified base packages (or the package of the annotated class if none is specified), registering them as beans in the IoC container. @EnableAutoConfiguration triggers Spring Boot’s auto‑configuration by importing AutoConfigurationImportSelector, which reads META-INF/spring.factories files via SpringFactoriesLoader to obtain a list of candidate configuration classes. The selector filters these candidates based on classpath presence, existing beans, and exclusion rules before returning them to the Spring context.
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!this.isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
AutoConfigurationMetadata metadata = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);
AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
configurations = this.removeDuplicates(configurations);
Set<String> exclusions = this.getExclusions(annotationMetadata, attributes);
this.checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = this.filter(configurations, metadata);
this.fireAutoConfigurationImportEvents(configurations, exclusions);
return StringUtils.toStringArray(configurations);
}The SpringFactoriesLoader utility loads the mapping from a factory interface to its implementation classes defined in spring.factories:
public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) {
String factoryClassName = factoryClass.getName();
return loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList());
}When the MyBatis starter is on the classpath, its spring.factories entry registers
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration. This configuration class is activated only if SqlSessionFactory and SqlSessionFactoryBean are present and a DataSource bean exists, and it defines beans such as SqlSessionFactory, SqlSessionTemplate, and related interceptors.
@Configuration
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
@ConditionalOnBean(DataSource.class)
@EnableConfigurationProperties(MybatisProperties.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class MybatisAutoConfiguration {
private final MybatisProperties properties;
private final Interceptor[] interceptors;
private final ResourceLoader resourceLoader;
private final DatabaseIdProvider databaseIdProvider;
private final List<ConfigurationCustomizer> configurationCustomizers;
// bean definitions omitted for brevity
}By simply adding the mybatis-spring-boot-starter dependency, all required MyBatis components are auto‑configured, demonstrating how Spring Boot’s starter mechanism eliminates the need for extensive XML configuration and lets developers focus on business logic.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
