第二课 从零开始学Spring boot 之 Hello World

 介绍

       以前spring开发需要配置一大堆的xml,后台spring加入了annotaion,使得xml配置简化了很多。

       Spring开了一个新的model ------ spring boot,主要思想是降低spring的入门,使得新手可以以最快的速度让程序在spring框架下跑起来。

       下面就开始我们的第一个spring boot的demo吧!!!

      1、新建一个maven  java工程。

首先使用IDEEclipse,MyEclipse)工具新建一个Maven工程,可以是Maven Java Project,也可以是Maven Web Project    

     2、在pom.xml文件中添加Spring Boot Maven依赖。

pom.xml中引入spring-boot-start-parent,它可以提供dependency management,也就是说依赖管理,

引入以后在申明其它dependency的时候就不需要version了,后面可以看到。

<!-- spring boot 父节点依赖,引入之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加 -->
   <!-- 1.添加依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>

<!--2、指定jdk版本,我们使用1.8,默认是1.6 -->
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

<!--3、因为我们开发的是web工程,所以需要在pom.xml中引入spring-boot-starter-web,
spring官方解释说spring-boot-start-web包含了spring webmvc和tomcat等web开发的特性 -->

<dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
</dependencies>

     3、编写启动类。 在启动类申明让 spring boot 自动给我们配置 spring 需要的配置,比如: @SpringBootApplication

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 *使用@SpringBootApplication制定这是一个springboot的应用程序
 */
@SpringBootApplication
public class App {
    public static void main( String[] args ){
        SpringApplication.run(App.class, args);
    }
}

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
	@RequestMapping("/test")
	public String test(){
	    return "test!!!";
	}
}


4、运行程序

第一种运行方式:右键Run As -> Java Application

第二种方式:右键project – Run as – Maven build – Goals里输入spring-boot:run ,然后Apply,最后点击Run

第三种方式:命令启动  打开命令窗口 — 进入项目目录 — mvn clean package — java -jar ./target/demo.jar

之后打开浏览器输入地址:http://127.0.0.1:8080/ 就可以看到“test!!!”了。

    5、Hello之Maven Run Application

       如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。如果使用maven spring-boot:run的话是不需要此配置的。(我在测试的时候,如果不配置下面的plugin也是直接在Main中运行的。)

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

 

   6、springboot 热部署

          启动springboot之后,如果我们每次更改controller里面的内容,就得重启springboot,岂不是烦死?别担心,引用了热部署的依赖,就解决此问题啦。

 如下:在pom.xml里面添加热部署依赖。

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

        <dependencies>

<!--springloaded 热部署依赖包 -->

    <dependency>

<groupId>org.springframework</groupId>

<artifactId>springloaded</artifactId>

<version>1.2.4.RELEASE</version>

    </dependency>

</dependencies>

<executions>

    <execution>

<goals>

    <goal>repackage</goal>

</goals>

<configuration>

<classifier>exec</classifier>

</configuration>

    </execution>

</executions>

 </plugin>
       </plugins>
</build>

如果是使用spring-boot:run的话,那么到此配置结束

       如果使用的run as – java application的话,则需要spring-loader-1.2.4.RELEASE.jar下载下来,放到项目的lib目录中,

    然后把Eclipserun参数里VM参数设置为:-javaagent:.\lib\springloaded-1.2.4.RELEASE.jar -noverify

    然后启动就可以了,这样在run as的时候,就可以能进行热部署了。


      到此为止,demo成功啦。怎么样,是不是感觉特快速、简洁呢。


猜你喜欢

转载自blog.csdn.net/gonghua0502/article/details/77005477