SpringBoot将所有Spring相关的配置集成化了,简化了配置。

快速入门demo

1.创建一个正常的maven项目

2.在pom文件中添加相关的支持

  <!-- 获取默认的配置 例如:引入jar的版本,默认编码等 -->

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

在<dependencies>中添加如下依赖(用不到的可以暂时未添加)

    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
    </dependency>

    <!-- spring boot 依赖jar包 -->
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- spring boot 热部署依赖jar包 -->
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-devtools</artifactId>
         <optional>true</optional>
         <scope>true</scope>
    </dependency>

     <!--对templates模板的支持-->
     <!-- 注意:添加对templates模板的支持与jsp的支持可能存在冲突,默认首先解析 html文件  -->
    <dependency>
         <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

在<build>中添加
     <!-- spring boot 编译插件 -->
  <plugins>
       <plugin>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-maven-plugin</artifactId>
           <configuration>
                  <fork>true</fork><!--注意此处要设置为true 热部署使用-->
           </configuration>
       </plugin>
  </plugins>

3.在项目包的最外层创建启动类MyApplication.java,该类中main方法为整个项目的入口。

4.特别注意:该启动类的位置,确保在整个项目类的最外层。具体如下图所示:

SpringBoot笔记(一)---- 入门笔记

5.启动类要标记注解@SpringBootApplication,具体启动类书写如下:

SpringBoot笔记(一)---- 入门笔记

6.右键run as,出现如下Spring图标并且启动不报错,即为配置成功。

SpringBoot笔记(一)---- 入门笔记

 特别注意:启动报错可能是入口类没有写注解。

7.更改相关配置

在resources文件夹下创建file类型的application.properties文件,用来完成spring boot项目的相关配置问题(数据源、集成mybatis、端口、springMVC 的前后缀等)。可以在该文件夹下尝试修改默认端口号(默认8080)配置

SpringBoot笔记(一)---- 入门笔记

重新run as,即可看到默认端口号变化

SpringBoot笔记(一)---- 入门笔记

8.编码并访问项目

在ctrl包下创建BootCtrl,代码如***意注解)

SpringBoot笔记(一)---- 入门笔记

访问

SpringBoot笔记(一)---- 入门笔记

源码下载https://download.csdn.net/download/qq_37918817/10612992

相关文章: