SpringBoot学习日记1 第一个SpringBoot程序
文章目录
SpringBoot学习日记
简单的Hello Word小程序
-
配置pom引入依赖
<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> -
编写代码
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这个页面了
-
分析
@RestController和@EnableAutoConfiguration、@RequestMapping
-
@RestController主要的作用是告知Spring渲染结果直接返回给调用者。==Json数据==相当于
@ Controller @ ResponseBody -
@RequestMapping就是在SpringMVC中作为路由功能的注解。 -
@EnableAutoConfigurationSpring Boot会通过pom.xml文件的依赖来自动配置,由于Spring-boot-starter-web中配置了Tomcat和SpringMVC,自动配置会配置为Web应用。
-
SpringBoot热部署:
<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>
分析pom.xml
依赖的关系
<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版本仲裁中心==
所以以后的导入依赖是不用写具体版本号的。
导入的依赖:
<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就可以了
@SpringBootApplication:
包含:
@Component:定义为一个组件可以被bean扫描器扫描出来
@ComponentScan:扫描被注解的对象。
@EnableAutoConfiguration:自动配置,自动导入
1 自动导入
@Import(EnableAutoConfigurationImportSelector.class)
导入SpringBootApplication注解修饰类的包名里面的所有的子包全部导入。
2 自动配置
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中
Spring Initializer
生成Boot项目的目录:
- static :保存静态文件;js,css,png
- templates : 保存模板页面;(默认不支持jsp,可以使用模板引擎)
- application.properties : Spring Boot的配置文件,默认配置文件可以在此文件中更改。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论