美文网首页
2.跟我学SpringBoot-第一个应用

2.跟我学SpringBoot-第一个应用

作者: 孔垂云 | 来源:发表于2017-12-03 14:11 被阅读0次

万事开头难,一直在提SpringBoot,怎么从头开发一个应用呢,本节将要着重讲这个。

maven依赖

SpringBoot的依赖很多,这里先用父模块来定义整个的依赖。

公共的pom.xml

<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.critc</groupId>
    <artifactId>java_springboot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>chapter1_first</module>
    </modules>
    <packaging>pom</packaging>

    <name>java_springboot</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
        <jdk.version>1.8</jdk.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <finalName>java_springboot</finalName>
        <defaultGoal>compile</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

这里面定义了父pom的配置,后续的module只需要继承该pom即可。

这里说一下和普通引用不同的地方,SpringBoot的引用,直接是一个组的方式,也是以pom的方式直接引用SpringBoot的所有组件,包括web、jdbc、mq、redis等等。

新建chapter1_first模块

模块目录如下:


模块目录.png
<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

增加应用启动的依赖即可。

启动类

/**
 * 第一个SpringBoot应用
 */
@SpringBootApplication
public class FirstApplication {
    public static void main(String[] args) {
        SpringApplication.run(FirstApplication.class, args);

    }
}

右键启动即可,这样第一个应用就开发完了,相当简单,为了测试一下,可以编写一个helloWord。

新建类FirstController

@RestController
public class FirstController {

    @RequestMapping("/hello")
    public String hello() {
        return "Hello SpringBoot";
    }
}

这个类也很简单,也是简单的Spring Controller类,不同的是controller注解是@RestController,而不是普通的@Controller

还有一点需要注意,实际开发中所有service、controller都要在Application该类的下一级,原因就是SpringBoot默认包扫描是以SpringBoot启动类的包为起点的。

右键执行main方法,即启动应用


SpringBot启动.png

在浏览器地址栏输入http://localhost:8080/hello,将会看到请求的输出:

第一个请求.png

至此一个最初的SpringBoot应用就开发完了,非常神奇,一个配置文件都没有,以后要是再说java臃肿的话,可以拿板砖拍他了。

启动注解@SpringBootApplication

通过@SpringBootApplication 这个注解可以把相关启动依赖类都注入进来,还包括读取基础配置文件等等,初学时先不要管那么多,等慢慢了解后,再来深入研究启动类的作用。

源码下载

本例子详细源码

相关文章

网友评论

      本文标题:2.跟我学SpringBoot-第一个应用

      本文链接:https://www.haomeiwen.com/subject/zpifzttx.html