spring boot简化了spring的开发,是J2EE一站式解决方案。css
入门容易,精通难;由于不少事情是spring boot自动完成的。html
martin flow 《Microservices》java
/** * @SpringBootApplication 用来告诉程序这是一个 spring boot 应用 */ @SpringBootApplication public class Helloworld { public static void main(String[] args) { // 启动Spring应用 SpringApplication.run(Helloworld.class, args); } }
controller类spring
@Controller public class HelloController { @ResponseBody @RequestMapping("/hello") public String hello(){ return "Hello world"; } }
<!-- spring boot 全部spring boot starter的父项目 是真正管理spring boot应用中全部依赖版本 是spring boot的版本仲裁中心(决定依赖库的版本) 之后咱们导入依赖默认是不须要写版本的(可是那些没有在父项目中管理的依赖天然是须要声明版本号的)--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <dependencies> <!--开发web应用须要的库--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!--将应用打包为jar包--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
/** * @SpringBootApplication 用来告诉程序这是一个 spring boot 应用 */ @SpringBootApplication public class Helloworld { public static void main(String[] args) { // 启动Spring应用 SpringApplication.run(Helloworld.class, args); } }
@SpringBootApplication 注解标注在某个类上的时候,说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用。该注解为组合注解,由如下注解组成:springboot
// 指定该注解能够给一个类型进行注解,好比类、接口、枚举 @Target(ElementType.TYPE) // 注解能够保留到程序运行的时候,它会被加载进入到 JVM 中,因此在程序运行时能够获取到它们。 @Retention(RetentionPolicy.RUNTIME) // 它的做用是可以将注解中的元素包含到 Javadoc 中去。 @Documented // 若是一个超类被 @Inherited 注解过的注解进行注解的话,那么若是它的子类没有被任何注解应用的话,那么这个子类就继承了超类的注解。 @Inherited // 代表这个类是一个SpringBoot的配置类 @SpringBootConfiguration // 告诉SpringBoot开启自动配置功能,自动配置才会生效 @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication {}
@SpringBootConfiguration 标记在某个类上时,代表这个类是一个SpringBoot的配置类。其组成为:app
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration {}
其中,@Configuration注解是Spring框架中基本的一个注解,在配置类上标记该注解,代表这个类是Spring的配置类。框架
@EnableAutoConfiguration告诉SpringBoot开启自动配置功能,这样自动配置才能生效。其组成为:jsp
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration {}
其中 @AutoConfigurationPackage自动配置包,将主配置类(即@SpringBootApplication标注的类)所在包及其下面全部子包里面的全部组件扫描到Spring容器中。其组成为:maven
@Import(AutoConfigurationPackages.Registrar.class) public @interface AutoConfigurationPackage {}
@Import为Spring的底层注解,代表给容器中导入一个组件,导入的组建由AutoConfigurationPackages.Registrar类提供。
@Import(AutoConfigurationImportSelector.class) 代表开启自动导入哪些组建的选择器;
AutoConfigurationImportSelector.class将要导入的组件以全类名的方式返回,这些容器会被导入到Spring容器。会给容器中导入很是多的自动配置类(通常以xxxAutoConfiguration结尾)。这些自动配置类的做用就是给容器中导入场景所需的自动配置,有了自动配置类,就能够不用手动写配置了
自动配置类是怎么找到的呢?
AutoConfigurationImportSelector - selectImports(AnnotationMetadata annotationMetadata) - List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes); // 返回全部配置,对应上面的图 - List<String> configurations = SpringFactoriesLoader.loadFactoryNames( getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()); - protected Class<?> getSpringFactoriesLoaderFactoryClass() { return EnableAutoConfiguration.class; } - String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; - Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");
从类路径下(如引用的各个jar包)的 META-INF/spring.factories中获取 EnableAutoConfiguration制定的值(自动配置类),这些值做为自动配置类导入到容器中,自动配置类就生效了,帮咱们进行自动配置工做。之前咱们须要本身配置的类,spring boot都自动配置了:
spring-boot-autoconfigure包中,包含了J2EE 全部整合方案和全部自动配置。
org\springframework\boot\spring-boot-autoconfigure\1.5.9.RELEASE\spring-boot-autoconfigure-1.5.9.RELEASE.jar!\org\springframework\boot\autoconfigure
resources 文件夹中目录结构: