[关闭]
@zhuanxu 2018-02-26T12:53:38.000000Z 字数 2864 阅读 1244

Attack-spring-boot-1:spring-boot 零起步笔记

spring-boot


看了好久spring-boot感觉不得要领,不知道为啥,想了下感觉是因为自己看好多教程就是一步一步跟着,但是不知道为何。

先来第一步:如何创建一个最简单的spring-boot项目。

第一步:创建maven项目

然后跟着步骤一步一步填写。
第二步:修改pom文件
可以到https://projects.spring.io/spring-boot/中复制依赖。

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.0.RC2</version>
  5. </parent>
  6. <dependencies>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-web</artifactId>
  10. </dependency>
  11. </dependencies>
  12. <repositories>
  13. <repository>
  14. <id>spring-milestones</id>
  15. <name>Spring Milestones</name>
  16. <url>https://repo.spring.io/libs-milestone</url>
  17. <snapshots>
  18. <enabled>false</enabled>
  19. </snapshots>
  20. </repository>
  21. </repositories>
  22. <pluginRepositories>
  23. <pluginRepository>
  24. <id>spring-milestones</id>
  25. <name>Spring Milestones</name>
  26. <url>https://repo.spring.io/libs-milestone</url>
  27. <snapshots>
  28. <enabled>false</enabled>
  29. </snapshots>
  30. </pluginRepository>
  31. </pluginRepositories>
  32. <build>
  33. <plugins>
  34. <plugin>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-maven-plugin</artifactId>
  37. </plugin>
  38. </plugins>
  39. </build>

上面因为还是RC版本,所有要放入milestone repository。

上面配置中几个有意思的地方:

Spring Boot父级依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.0.RC2</version>
  5. </parent>


我们会发现spring-boot-starter-parent只有一个pom文件,里面的内容:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-dependencies</artifactId>
  4. <version>2.0.0.RC2</version>
  5. <relativePath>../../spring-boot-dependencies</relativePath>
  6. </parent>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <packaging>pom</packaging>

他的parent是spring-boot-dependencies,里面非常有意思:

  1. <properties>
  2. <activemq.version>5.15.3</activemq.version>
  3. <antlr2.version>2.7.7</antlr2.version>
  4. <appengine-sdk.version>1.9.62</appengine-sdk.version>
  5. <artemis.version>2.4.0</artemis.version>
  6. <aspectj.version>1.8.13</aspectj.version>
  7. <assertj.version>3.9.0</assertj.version>
  8. .....
  9. </properties>

可以看到里面定义了各种包的版本。

spring-boot-starter-xx

spring-boot-starter-xx 简化了大量配置,让我们能够开箱即用,其本质是一个Maven项目对象模型(Project Object Model,POM),定义了对其他库的传递依赖,这些东西加在一起即支持某项功能。

Spring Boot Maven插件

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.springframework.boot</groupId>
  5. <artifactId>spring-boot-maven-plugin</artifactId>
  6. </plugin>
  7. </plugins>
  8. </build>

Spring Boot Maven插件提供了许多方便的功能:

Spring Boot Maven plugin的5个Goals

程序启动

程序启动方式有3:
1. idea 直接执行

2. 运行“mvn package”进行打包,再使用“java -jar”命令就可以直接运行
3. 使用命令 mvn spring-boot:run 直接启动

总结

本文是自己的spring-boot入门记录,以后会持续更新,欢迎关注。

你的鼓励是我继续写下去的动力,期待我们共同进步。
这个时代,每个人都是超级个体!关注我,一起成长!

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注