springBoot学习
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
@SpringBootApplicationpublicclassHelloWorldSpringbootApplication{
public static void main(String[]args){
SpringApplication.run(HelloWorldSpringbootApplication.class,args);
}}
@Controller
public clas sMainController{
@ResponseBody
@RequestMapping("/hello")
publicStringhelloWorld(){
return"hello world quick";
}}
localhost:8080/hello
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork : 如果没有该项配置,肯定devtools不会起作用,即应用不会 restart 这个要手动加进去 --><fork>true</fork>
</configuration>
</plugin>
</plugins></build>
(2)打jar包(在侧边栏的maven->lifeCycle->package(双击))
(4)就可以在cmd通过 命令java -jar jar包名 来运行了(与运行主程序是一样的结果)
父项目:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
他的父项目是:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
他来真正管理SpringBoot应用里面所有的依赖版本
所以说,他是SpringBoot的版本仲裁中心:
我们以后在导入依赖的时候,是不需要写版本号的(没有在其中声明版本号的,需要自行声明)。
导入的依赖:
spring-boot-starter-web:
spring-boot-starter 是springboot的场景启动器,-web则是导入web模块正常运行所需要的依赖。
springboot将所有的功能场景都抽取出来,做成一个个的starter(启动器),我们在应用中需要什么场景的启动器,那么我们就导入对应的starter,那么该场景依赖的所有依赖也都会导入进来。
@SpringBootApplicationpublicclassHelloWorldSpringbootApplication{publicstaticvoidmain(String[]args){SpringApplication.run(HelloWorldSpringbootApplication.class,args);}}
这里的注解@SpringBootApplication,有了这个注解,才可以正常的调用run方法来启动应用。
@SpringBootApplication:这个注解标注在哪个类上,说明这个类则是springboot应用的主配置类,springboot就应该运行这个类的main方法来启动springboot应用。
@SpringBootApplication:这是一个组合注解
@SpringBootConfiguration:这是一个SpringBoot的配置注解
这个注解标注在某个类上,就表示这是一个springBoot的配置类
点进@SpringBootConfiguration,你会发现他也是组合注解,其中@Configuration是spring底层的配置类注解
@Configuration:这个表示配置类----配置文件(对应),配置类也是容器中的组件
@EnableAutoConfiguration:开启自动配置
以前需要我们自动配置的东西,现在springboot帮我们自动配置,@EnableAutoConfiguration告诉springboot开启自动配置。
点进@SpringBootConfiguration,有一个@AutoConfigurationPackage(自动配置包)注解,点进,你会发现@Import({Registrar.class})有这么一个注解,这也是spring底层的注解,给容器导入组件,由他来指定该导入哪些组件。
@AutoConfigurationPackage:将主配置类所在包及所有子包下面的所有组件扫描到spring容器中
@Import({AutoConfigurationImportSelector.class}):给容器中导入组件
AutoConfigurationImportSelector:导入哪些组件的选择器
将所有需要导入的组件以全类名的方式返回,这些组件就会被添加到组件中。
会给容器中导入好多的自动配置类(xxxAutoConfiguration),就是给容器中导入这个场景需要的所有组件,并配置好这些组件。
注:ResponseBody这个注解是把方法返回的结果直接写给数据库(如果是对象转为json数据)
8.使用Spring Initializer创建Spring Boot项目
选择我们需要的模块,联网来生成SpringBoot项目:
生成的springBoot项目:
主程序已经生成好了,只需要我们自己来创建逻辑
其中resources文件夹:
static:存放所有的静态资源js,css,images;
templates:存放所有的模版资源,springBoot默认jar包使用嵌入式的tomcat,不支持jsp页面;
application.properties:是springBoot应用的配置文件,可以用来修改一些默认设置