spring boot 搭建

**摘要:**Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者

在这里我大概讲述3种方式搭建spring boot基本环境

1.普通maven项目改造

idea新建一个maven项目,这里就不说了,建好之后,pom.xml改造,我这里只加入了web的jar

<?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.yf</groupId>
    <artifactId>test_spring</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置文件中,加入的配置其实就是上面写了注释的,其他的是建项目自动生成出来的
接下来在resources下新建 application.properties文件,这个文件是你用来配置其他设置的,这里不细说,新建如下图目录结构,这个随你自己,
这是注意的是你的启动类的位置问题
比如我上面的pom.xml文件的 com.yf是这样子的,那我的Application.java的目录结构是com.yf.Application.java这个类启动时是自动扫描同级目录和同级目录下的java类

这里写图片描述

Application.java类如下

@SpringBootApplication
public class Application {
     public static void main(String args[]) {
       SpringApplication.run(Application.class, args);
     }
}

这里记得加上@SpringBootApplication注解,这个注解相当于这3个注解@Configuration @EnableAutoConfiguration @ComponentScan

我们在web包下新建 HelloController.java

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
       return "hello";
    }
}

此时启动Application.java类 浏览器输入 http://localhost:8080/hello 可以看到返回值
更多可以访问官网 https://projects.spring.io/spring-boot/

2.通过cmd窗口新建

这个首先下载spring boot CLI ,点击上面的官网,在最底下有个 install the Spring Boot CLI. 点击下载,解压,配置环境变量,直接 path 后面加入 D:\Workspace\spring_boot\spring-1.5.9.RELEASE\bin(你自己的本地地址)
cmd 窗口输入 spring –version 查看版本,spring会有命令说明

这里写图片描述
如图所示,安装成功
测试 ,输入
spring init -d=web,camel –build maven test-cli
新建了一个test-cli项目,maven结构的,加入了web,camel依赖包,导入工具即可
这里写图片描述

3.页面直接生成在导入idea工具

页面输入 http://start.spring.io/ ,如下

这里写图片描述

当然还有其他方式新建,比如直接idea工具就可以,这里就不说了

猜你喜欢

转载自blog.csdn.net/fengchen0123456789/article/details/79218563