配置pom引入依赖css
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
编写代码java
import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } }
最后经过这两个简单的配置,就能够使用浏览器访问localhost:8080 访问到 hello word这个页面了web
分析spring
@RestController和@EnableAutoConfiguration、@RequestMapping小程序
@RestController
主要的做用是告知Spring渲染结果直接返回给调用者。==Json数据==浏览器
至关于app
@ Controller @ ResponseBody
@RequestMapping
就是在SpringMVC中做为路由功能的注解。@EnableAutoConfiguration
Spring Boot会经过pom.xml文件的依赖来自动配置,因为Spring-boot-starter-web中配置了Tomcat和SpringMVC,自动配置会配置为Web应用。<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin </artifactId> <dependencies> <!--springloaded hot deploy --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.4.RELEASE</version> </dependency> </dependencies> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions> </plugin>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> 依赖于下面: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.9.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>
管理SpringBoot全部依赖,==SpringBoot版本仲裁中心==jsp
因此之后的导入依赖是不用写具体版本号的。maven
导入的依赖:ide
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
Spring-boot-starter-==web==:导入Web应用的时候,导入Web须要用到的依赖。
Spring-boot-starter-x:Spring Boot的场景启动器(里面包含了多个整合好的依赖)。
SpringBoot将全部的功能场景都抽取出来作成Staters 只须要引入Staters就能够了
包含:
@Component:定义为一个组件能够被bean扫描器扫描出来
@ComponentScan:扫描被注解的对象。
@EnableAutoConfiguration:自动配置,自动导入
@Import(EnableAutoConfigurationImportSelector.class)
导入SpringBootApplication注解修饰类的包名里面的全部的子包所有导入。
EnableAutoConfigurationImportSelector 继承于 AutoConfigurationImportSelector
@Override public String[] selectImports(AnnotationMetadata annotationMetadata) { List<String> configurations = getCandidateConfigurations(annotationMetadata, } /** 扫描META-INF/spring.factories中获取EnableAutoConfiguration的值再包装成Properties,从properties中获取到EnableAutoConfiguration.class对应的全限定名类名,添加进入容器中。 */ protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> configurations = SpringFactoriesLoader.loadFactoryNames( getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()); }
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.calss ,classLoader);
获取类路径下META-INF/spring.factories中获取EnableAutoConfiguration的值,再将这些值做为自动配置类导入到容器中。
==结论==:@SpringBootApplication 被修饰的类为主配置类,将主配置类所在的包下的全部的子包里面的组件所有扫描到Spring容器中来,再从根目录下的文件中读取配置文件生成配置类,自动配置好。
JaveEE的总体整合和自动配置都在:pring-boot-autoconfigure-1.5.9.RELEASE.jar中
生成Boot项目的目录: