SpringBoot+Maven eclipse项目搭建

1、我们new一个maven项目

SpringBoot+Maven eclipse项目搭建

勾上create a simple project,个人感觉比较方便!

2、简单的填写一下就ok了

SpringBoot+Maven eclipse项目搭建

填完finish就完事了。

3、添加pom文件的依赖

SpringBoot+Maven eclipse项目搭建

比较重要就是spring-boot-starter-parent这个依赖,我用的是1.5.7,当然你可以选择最新的2.0版本。

pom:

<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.chris</groupId>
    <artifactId>SpringBoot.Test</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- spring热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>


4、我们run一下。

SpringBoot+Maven eclipse项目搭建

5、然后更新一下项目

SpringBoot+Maven eclipse项目搭建


6、我们需要建一个项目启动类

SpringBoot+Maven eclipse项目搭建

SpringBoot+Maven eclipse项目搭建

7、Application项目类

SpringBoot+Maven eclipse项目搭建

关于@SpringBootApplication:很多Spring Boot开发者总是使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 注解他们的main类。由于这些注解被如此频繁地一块使用(特别是你遵循以上最佳实践时),Spring Boot提供一个方便的 @SpringBootApplication 选择。该@SpringBootApplication 注解等价于以默认属性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 。(引用于SpringBoot官方文档)

@RestController 将这个类注册为一个controller,再写一个测试方法方便测试是否成功。

8、在resources下建一个config文件夹

SpringBoot+Maven eclipse项目搭建

再在config建一个名字为application.yml的springboot配置文件

9、设置一个端口号

SpringBoot+Maven eclipse项目搭建

10、在刚才建的Application.class启动类中右键run启动项目测试

SpringBoot+Maven eclipse项目搭建

11、我们输入刚才的的项目地址出现test说明项目启动成功

SpringBoot+Maven eclipse项目搭建

完整的项目结构:

SpringBoot+Maven eclipse项目搭建