zoukankan      html  css  js  c++  java
  • springboot 入门一 hello world!

    微服务框架springboot,目的是用来简化新Spring应用的初始搭建以及开发过程。简化的代价,就是约定俗成很多规则,比如默认读取的配置文件名是application.properties 必需在config目录下,启动类的扫描是平级及子目录。springboot并非是现有问题新的解决方案,而是一种为平台开发带来新的体验,简化繁杂的xml等各种变动不大的配置信息,约定优于配置。

    Boot对Spring应用的开发进行了简化,提供了模块化方式导入依赖的能力,强调了开发RESTful Web服务的功能并提供了生成可运行jar的能力,这一切都清晰地表明在开发可部署的微服务方面Boot框架是一个强大的工具。

    要实现一个url : http://localhost:8080/index  返回字符串:hello world!,

    以前的做法:配置web.xml spring-***.xml 再组合tomcat或jetty应用服务器。

    spring boot写法(maven项目)

    一、pom.xml引用包

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.6.RELEASE</version>
        </parent>

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>

    二、添加controller

    @RestController
    public class HomeController {
        @RequestMapping("index")
        public String index(){        
            return "hello world!";
        }
    }

    三、添加启动类

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

    四、运行启动类即可

    是不是很简化,全程没配置spring相关等xml文件。spring高版本越来越使用注解来代替xml配置。默认内置spring-boot-starter-tomcat应用,默认端口为8080

    @RestController是一个封装注解,集合@Controller+@ResponseBody  标识此类所有路由方法返回string

    @SpringBootApplication也是一个封装注解,@Configuration  @EnableAutoConfiguration   @ComponentScan

    @Configuration  标识类可以使用Spring IoC容器作为bean定义的来源

    @EnableAutoConfiguration  能够自动配置spring的上下文,通常会自动根据你的类路径和你的bean定义自动配置。

    @ComponentScan  会自动扫描指定包下的全部标有@Component的类,并注册成bean

    运行服务有三种方式

    1、运行启动类,直接跑main方法

    2、命令行中使用  mvn  spring-boot:run

    3、可生成单独执行的jar

      在pom.xml添加插件

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

      然后打成jar包:   mvn package

      执行:  java -jar spring-boot-1.0.0-SNAPSHOT.jar  即可。

  • 相关阅读:
    fatal: unable to access ‘https://github.com/Homebrew/brew/‘: Error in the HTTP2 framing layer
    leetcode题目两数和、最大字符串长度、中位数、最长回文子串
    zabbix5.x版本添加自定义监控+263邮件报警
    php过滤特殊字符方法
    js中e相关位置
    少有人走的路-心智成熟的旅程-斯科特·派克
    ios脱壳
    docker 容器开机自启
    Java8 Stream的使用
    Entity Frameworker 的核心 EDM(Entity Data Model)模式
  • 原文地址:https://www.cnblogs.com/song27/p/7513876.html
Copyright © 2011-2022 走看看