zoukankan      html  css  js  c++  java
  • Spring boot 零配置开发微服务

    20181229日星期六

    体验Spring boot 零配置开发微服务

    1.为什么要用Spring  boot

       1.1 简单方便、配置少、整合了大多数框架

       1.2 适用于微服务搭建,搭建的微服务与Spring clound进行完美融合,因为都是Spring家族

    2. Spring boot开发过程

    2.1 启动Idea

    2.2 创建Maven项目

    2.3 引入spring-boot-starter-web核心:Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container

    2.4 引入spring-boot-starter-test核心:Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito

    2.5 具体依赖如下:

    2.6 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.0.4.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.0.4.RELEASE</version>
        <scope>test</scope>
    </dependency>

    2.7 创建Spring boot启动主程序:package com.wuji.boot;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;

    @SpringBootApplication
    @ComponentScan(basePackages = {"com.wuji.controller"})
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class,args);
        }
    }

    2.8 编写HelloController:

    package com.wuji.controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

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

    2.9 运行http://localhost:8080/api/hello

    2.10 第一次写Spring boot程序运行一般会出现:whitelabel error page 这个错误,是因为默认没有加载控制器包。要在主程序加上@ComponentScan(basePackages = {"com.wuji.controller"})注解

    2.11 总结:整个程序只引用了两个核心包2.32.4,没有其它配置,就可以启动Restful服务。所以证实了Spring boot基本零配置来开发微服务。

  • 相关阅读:
    链接器工具错误 LNK2026 XXX模块对于 SAFESEH 映像是不安全的
    无法定位程序输入点 _glutCreateWindowWithExit于动态链接库glut32.dll上
    Error:“应用程序无法正常启动(0xc000007b)。请单击“确定”关闭应用程序。”
    虚函数和纯虚函数的区别
    VS2010和matlab2010混合编程中char16_t重定义的问题
    笔记本电脑关闭小键盘(即打字按P出现星号键)
    WIN7系统下U盘安装Ubuntu双系统
    The Basics of 3D Printing in 2015
    3D建模与处理软件简介
    win7-32 系统 + VS2010 配置 glew
  • 原文地址:https://www.cnblogs.com/javajiuyangzhenjing/p/10195326.html
Copyright © 2011-2022 走看看