简易Spring-boot项目的搭建demo

最近刚开始学习Spring-boot框架,从最开始搭建一个Maven工程--配置文件--测试demo--启动Spring—boot项目。

1.新建一个maven project(Create a simple project)

2.Next   

填写好项目名称及打包方式,Finish

3.这时构建完项目后pom.xml文件会报错,则这时候需要我们导入依赖的jar包

<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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    
    <groupId>cn.itcast.springboot</groupId>
    <artifactId>itcast-springboot</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp-spring</artifactId>
            <version>0.8.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <!-- 连接池 -->
        <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp-spring</artifactId>
            <version>0.8.0.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- 资源文件拷贝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- java编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!-- 配置Tomcat插件 -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

4.这时候pom.xml文件可能还会有报错,如果有web.xml is missing and <failOnMissingWebXml> is set to true错误,则看如下链接有解决方案:https://blog.csdn.net/qq_38383402/article/details/82625095

5.此处注意要设置Spring boot的parent

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.2.RELEASE</version>

</parent>

说明:Spring boot的项目必须要将parent设置为spring boot的parent,该parent包含了大量默认的配置,大大简化了我们的开发。

设置Spring boot的web支持

<dependency>

        <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-web</artifactId>

</dependency>

添加Spring boot的插件

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

6.接下来我们可以测试写第一个Spring boot应用

@Controller

@SpringBootApplication

@Configuration

public class HelloApplication {

    @RequestMapping("hello")

    @ResponseBody

    public String hello(){

        return "hello world!";

    }

    public static void main(String[] args) {

        SpringApplication.run(HelloApplication.class, args);

    }

}

代码说明:

    1、@SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置。;

    2、@Configuration:这是一个配置Spring的配置类;

    3、@Controller:标明这是一个SpringMVC的Controller控制器;

    4、main方法:在main方法中启动一个应用,即:这个应用的入口;

 

7.在Spring Boot项目中,启动的方式有两种,一种是直接run Java Application另外一种是通过Spring Boot的Maven插件运行。

看到有  Started HelloApplication in 6.148 seconds (JVM running for 6.762)  说明Spring boot项目启动成功。

第二种方式启动Spring boot项目

执行Run

控制台输出 Started HelloApplication in 5.111 seconds (JVM running for 48.644)  则Spring boot 项目启动成功。

8.访问页面查看效果

http://localhost:8080/hello

如果看到页面显示 Hello World! 则表示Spring boot项目成功启动并能访问。

猜你喜欢

转载自blog.csdn.net/qq_38383402/article/details/82667794