用Maven我的项目创立Spring利用

  1. 创立maven工程
  2. 在pom.xml中导入Spring Boot相干依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <!--SpringBoot版本--> <version>1.5.9.RELEASE</version></parent><dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency></dependencies>

3.编写主程序

package hello;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class springHello {    public static void main(String[] args){        SpringApplication.run(springHello.class,args); }}

4.编写相干的Controler、Service

package hello.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@org.springframework.stereotype.Controllerpublic class Controller {    @ResponseBody @RequestMapping("/hello")    public String hello(){        return "Hello World!"; }}

5.测试启动main函数
6.部署
(1)首先把maven插件依赖写入pom.xml,这个maven插件能够把我的项目打包为jar包

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins></build>

(2)maven-->申明周期-->package,失去jar包

(3)java -jar 我的项目名

总结:创立Maven我的项目-->导入父我的项目和用到模块的场景启动器-->写主程序-->写业务逻辑

Spring Boot原理

  • 流程:写主程序启动SpringBoot利用-->依照业务逻辑写Controller、Service,不再须要配置其余货色

为何SpringBoot不须要配置其余货色:剖析pom.xml

  1. 点击pom.xml的父我的项目spring-boot-starter-parent,进入后点击父我的项目的父我的项目spring-boot-dependencies,发现里边定义了各种依赖的版本。即父父我的项目真正治理SpringBoot的所有依赖,是版本仲裁核心。所以咱们导入依赖时默认不须要写版本(父父我的项目中没定义的除外)
  2. 导入依赖(场景启动器)。springboot将所有性能场景抽取进去,做成starter。咱们须要啥性能,就导入相干的starter

用Spring我的项目创立springboot利用

  • 抉择好导入的场景启动器后,父我的项目和场景启动器主动增加,主程序也有了。咱们只有分心写业务逻辑就行了
  • 有三个文件是没用的,能够删除掉
  • resources中有三个文件夹

(1)static:保留所有的动态资源,如js、css、图片等
(2)templates:保留所有的模板页html。springboot默认的jar包应用嵌入式tomcat,不反对jsp页面,但咱们可应用模板引擎(freemarker、thymeleaf)解决问题
(3)application.properities:springboot利用的配置文件,咱们能够写他来批改默认设置