利用Maven快速创建一个简单的spring boot 实例

Spring Boot的好处:spring boot 大大减少了 使用spring的配置 和大量 xml 文件,并有效解决的项目之间的依赖问题,为想使用 spring项目 大大减轻的工作量

1.先创建一个Maven项目

2.配置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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>SpringBootDemo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringBootDemo Maven Webapp</name>
  <url>http://maven.apache.org</url>
        <!-- parent 对应的父依赖,自动为你添加常用的容器依赖 -->
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
      </parent>
      <dependencies>
          <!-- 开发web项目需要下面jar包 -->
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
      </dependencies>
  <build>
    <finalName>SpringBootDemo</finalName>
  </build>
</project>

3.简单的Application类

@EnableAutoConfiguration:自动载入应用程序所需的所有Bean

SpringApplication.run()将引导我们的应用,启动Spring,相应地启动被自动配置的Tomcat web服务器

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class Example {
    
    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class, args);
    }
}

然后Run As启动

访问:

原文地址:https://www.cnblogs.com/-scl/p/7518272.html