关键pom配置 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</a
关键pom配置
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency><plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins>
hello world
@SpringBootApplicationpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}
@SpringBootApplication背后
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)@[email protected]@org.springframework.boot.SpringBootConfiguration@org.springframework.boot.autoconfigure.EnableAutoConfiguration@org.springframework.context.annotation.ComponentScan(excludeFilters = {@org.springframework.context.annotation.ComponentScan.Filter(type = org.springframework.context.annotation.FilterType.CUSTOM, classes = {org.springframework.boot.context.TypeExcludeFilter.class})})
SpringBootConfiguration背后
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)@java.lang.annotation.Documented@org.springframework.context.annotation.Configuration
EnableAutoConfiguration背后
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)@[email protected]@org.springframework.boot.autoconfigure.AutoConfigurationPackage@org.springframework.context.annotation.Import({org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector.class})
最关键的三个注解
@Configuration
是javaConfig形式的spring配置
@EnableAutoConfiguration
借助@Import,将所有符合自动配置条件的bean定义加载到IoC容器中
SpringFactoriesLoader是Spring框架的扩展方案,配置文件是META-INF/spring.factories
在EnableAutoConfiguration中,提供了一种配置查找的功能支持,查找所有classpath中的META-INF/spring.factories配置文件,通过配置项反射为javaConfig,配置IoC容器
@ComponentScan
自动扫描并加载符合条件的组件或bean定义,并加载到容器中