閱讀81 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Spring Boot 快速入門

Spring Boot官方介紹

  • Spring Boot makes it easy to create Spring-powered, production-grade applications and services with absolute minimum fuss. It takes an opinionated view of the Spring platform so that new and existing users can quickly get to the bits they need.
  • You can use Spring Boot to create stand-alone Java applications that can be started using java -jar or more traditional WAR deployments. We also provide a command line tool that runs spring scripts.

Spring Boot主要優點:

  • 創建獨立的Spring應用程序
  • 嵌入的Tomcat,無需部署WAR文件
  • 簡化Maven配置
  • 自動配置Spring
  • 提供生產就緒型功能,如指標,健康檢查和外部配置
  • 無需編寫繁瑣的XML配置

快速創建Spring Boot項目
1. 訪問:https://start.spring.io/
2. 選擇構建工具Maven Project、Spring Boot版本1.5.4以及一些工程基本信息
image
3. 點擊Generate Project下載項目壓縮包
4. 解壓項目包,並用IDE以Maven項目導入

  • 項目結構
    image

  • pom.xml

    <![CDATA[
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.example</groupId>
    <artifactId>spring-boot-sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <name>spring-boot-sample</name>
    <description>Demo project for Spring Boot</description>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    
    <dependencies>
        <!--核心模塊,包括自動配置支持、日誌和YAML-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--熱啟動-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--測試模塊,包括JUnit、Hamcrest、Mockito-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
    <!-- 設定Maven倉庫 -->
    <repositories>
        <repository>
            <id>maven-aliyun</id>
            <name>maven-aliyun</name>
            <url>https://maven.aliyun.com/nexus/content/groups/public</url>
        </repository>
    </repositories>
    <!-- 設定plugin倉庫 -->
    <pluginRepositories>
        <pluginRepository>
            <id>maven-aliyun</id>
            <name>maven-aliyun</name>
            <url>https://maven.aliyun.com/nexus/content/groups/public</url>
        </pluginRepository>
    </pluginRepositories>
    </project>
    ]]>
    
  • SpringBootSampleApplication.java

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

    @RestController
    public class SampleController {
    
    @RequestMapping(value = "/hello")
    String hello() {
        return "hello world!";
    }
    }
    
  • SpringBootSampleApplicationTests

    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    @AutoConfigureMockMvc
    public class SpringBootSampleApplicationTests {
    
    @Autowired
    private MockMvc mvc;
    
    @Test
    public void contextLoads() {
        try{
            URI uri = new URI("/hello");
            MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON))
                    .andReturn();
            String context = mvcResult.getResponse().getContentAsString();
            System.out.println(context);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    }
    
  • application.properties

server.port=8000 #指定程序運行端口

image
大功告成!!!運行SpringBootSampleApplication的main方法SpringBoot即可啟動起來~
瀏覽器訪問https://localhost:8000/hello
image

運行測試類
image

歡迎支持
![image](https://yqfile.alicdn.com/0e0d205f04a71ec162349389f7064f2a84894f08.jpeg)

最後更新:2017-07-12 09:32:21

  上一篇:go  2017年免費OA係統如何正確選型?
  下一篇:go  最新的PHP視頻