Springboot学习笔记二
初始化的Spring Boot的框架基本没有东西,除了空目录外还包含以下内容:
pom.xml:Maven构建说明文件。
Chapter1Application.java:一个带有main()方法的类,用于启动应用程序(关键)。
Chapter1ApplicationTests.java:一个空的Junit测试类,它加载了一个使用Spring Boot字典配置功能的Spring应用程序上下文。
application.properties:一个空的properties文件,可以根据需要添加配置属性。
1.其中pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dudu</groupId>
<artifactId>chapter1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>chapter1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
其中几个重要的配置是:
Springboot父级依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
这块配置就是Spring Boot父级依赖,有了这个,当前的项目就是Spring Boot项目了,spring-boot-starter-parent是一个特殊的starter,它用来提供相关的Maven默认依赖,使用它之后,常用的包依赖可以省去version标签。关于Spring Boot提供了哪些jar包的依赖,可查看C:\Users\用户.m2\repository\org\springframework\boot\spring-boot-dependencies\1.5.1.RELEASE\spring-boot-dependencies-1.5.1.RELEASE.pom
<properties>
<!-- Dependency versions -->
<activemq.version>5.14.3</activemq.version>
<antlr2.version>2.7.7</antlr2.version>
<appengine-sdk.version>1.9.48</appengine-sdk.version>
<artemis.version>1.5.2</artemis.version>
<aspectj.version>1.8.9</aspectj.version>
<assertj.version>2.6.0</assertj.version>
<atomikos.version>3.9.3</atomikos.version>
<bitronix.version>2.1.4</bitronix.version>
<caffeine.version>2.3.5</caffeine.version>
<cassandra-driver.version>3.1.3</cassandra-driver.version>
<classmate.version>1.3.3</classmate.version>
<commons-beanutils.version>1.9.3</commons-beanutils.version>
<commons-collections.version>3.2.2</commons-collections.version>
<spring-data-releasetrain.version>Ingalls-RELEASE</spring-data-releasetrain.version>
......
</properties>
如果不想使用某个依赖默认的版本,还可以通过覆盖自己的项目中的属性来覆盖各个依赖项,例如,要升级到另一个Spring Data版本系列,可以将以下内容添加到pom.xml中。
<properties> <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version> </properties>
2.起步依赖 spring-boot-starter-xx
Spring Boot提供了很多”开箱即用“的依赖模块,都是以spring-boot-starter-xx作为命名的。举个例子来说明一下这个起步依赖的好处,比如组装台式机和品牌机,自己组装的话需要自己去选择不同的零件,最后还要组装起来,期间有可能会遇到零件不匹配的问题。耗时又消力,而品牌机就好一点,买来就能直接用的,后续想换零件也是可以的。相比较之下,后者带来的效果更好点(这里就不讨论价格问题哈),起步依赖就像这里的品牌机,自动给你封装好了你想要实现的功能的依赖。就比如我们之前要实现web功能,引入了spring-boot-starter-web这个起步依赖。

3.Springboot Maven插件
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
上面的配置就是Spring Boot Maven插件,Spring Boot Maven插件提供了许多方便的功能:
- 把项目打包成一个可执行的超级JAR(uber-JAR),包括把应用程序的所有依赖打入JAR文件内,并为JAR添加一个描述文件,其中的内容能让你用java -jar来运行应用程序。
- 搜索public static void main()方法来标记为可运行类。

浙公网安备 33010602011771号