资讯专栏INFORMATION COLUMN

Spring Boot (一)helloworld

go4it / 2178人阅读

摘要:第二个类级别注解是。将引导应用程序,启动,从而启动自动配置服务器。比如想使用不同版本的,具体如下在标签中还可以指定编译的版本和项目的编码格式指定项目编码为使用插件可以为项目提供的操作方式,的个,默认。

引言

Spring 框架对于很多 Java 开发人员来说都不陌生。Spring 框架包含几十个不同的子项目,涵盖应用开发的不同方面。如此多的子项目和组件,一方面方便了开发人员的使用,另外一个方面也带来了使用方面的问题。每个子项目都有一定的学习曲线。开发人员需要了解这些子项目和组件的具体细节,才能知道如何把这些子项目整合起来形成一个完整的解决方案。在如何使用这些组件上,并没有相关的最佳实践提供指导。对于新接触 Spring 框架的开发人员来说,并不知道如何更好的使用这些组件。Spring 框架的另外一个常见问题是要快速创建一个可以运行的应用比较麻烦。Spring Boot 是 Spring 框架的一个新的子项目,用于创建 Spring 4.0 项目。它可以自动配置 Spring 的各种组件,并不依赖代码生成和 XML 配置文件。Spring Boot 也提供了对于常见场景的推荐组件配置。Spring Boot 可以大大提升使用 Spring 框架时的开发效率,对于快速开发一个可运行的非大型的项目非常合适。

简介

从 Spring Boot 项目名称中的 Boot 可以看出来,Spring Boot 的作用在于创建和启动新的基于 Spring 框架的项目。它的目的是帮助开发人员很容易的创建出独立运行和产品级别的基于 Spring 框架的应用。Spring Boot 会选择最适合的 Spring 子项目和第三方开源库进行整合。大部分 Spring Boot 应用只需要非常少的配置就可以快速运行起来。

Spring Boot 包含的特性如下:

* 创建可以独立运行的 Spring 应用。
* 直接嵌入 Tomcat 或 Jetty 服务器,不需要部署 WAR 文件。
* 尽可能的根据项目依赖来自动配置 Spring 框架。
* 不需要传统的Sring项目繁多的 XML 配置文件。

通过 Spring Boot,创建新的 Spring 应用变得非常容易,而且创建出的 Spring 应用符合通用的最佳实践。

官方文档上有这样一段介绍:

Learn what you can do with Spring Boot ?

Spring Boot offers a fast way to build applications. It looks at your classpath and at beans you have configured, makes reasonable assumptions about what you’re missing, and adds it. With Spring Boot you can focus more on business features and less on infrastructure.
For example:

Got Spring MVC? There are several specific beans you almost always need, and Spring Boot adds them automatically. A Spring MVC app also needs a servlet container, so Spring Boot automatically configures embedded Tomcat.

Got Jetty? If so, you probably do NOT want Tomcat, but instead embedded Jetty. Spring Boot handles that for you.

Got Thymeleaf? There are a few beans that must always be added to your application context; Spring Boot adds them for you.

These are just a few examples of the automatic configuration Spring Boot provides. At the same time, Spring Boot doesn’t get in your way. For example, if Thymeleaf is on your path, Spring Boot adds a SpringTemplateEngine to your application context automatically. But if you define your own SpringTemplateEngine with your own settings, then Spring Boot won’t add one. This leaves you in control with little effort on your part.
Spring Boot doesn"t generate code or make edits to your files. Instead, when you start up your application, Spring Boot dynamically wires up beans and settings and applies them to your application context.

Spring Boot doesn"t generate code or make edits to your files. Instead, when you start up your application, Spring Boot dynamically wires up beans and settings and applies them to your application context.

代码示例

1.在IDEA中创建一个项目,和一个helloworld模块:

目录结构如图:



在pom.xml文件中引入Spring-boot的依赖:



    org.springframework.boot
    spring-boot-starter-parent
    1.5.7.RELEASE


4.0.0
jar




    
        org.springframework.boot
        spring-boot-starter-web
    




        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    com.lzumetal.springboot.helloworld.Application
                
            
        
    



再创建两个类:
第一个是Application.java,用来启动SpringBoot应用:

package com.lzumtal.springboot.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Application {

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

第二个类HelloController.java用来响应web请求:

package com.lzumetal.springboot.helloworld.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {


    @RequestMapping("/home")
    String home() {
        return "Hello World!";
    }


    @RequestMapping("/hello/{name}")
    String hello(@PathVariable("name") String name) {
        return "hello " + name + "!!!";
    }

}



2.运行

运行Aplication中的main()方法,控制台打印启动信息:



在浏览中访问:http://localhost:8080/home

访问:http://localhost:8080/hello/spring-boot

这样示例就成功运行了。

补充说明

1.@SpringbootApplication注解:

Spring Boot默认是扫描@SpringBootApplication注解的类(即启动类)的同包以及子包下的类。

使用@SpringbootApplication注解 可以解决根类或者配置类头上注解过多的问题,一个@SpringbootApplication相当于@Configuration,@EnableAutoConfiguration和@ComponentScan 并具有他们的默认属性值

@SpringBootApplication is a convenience annotation that adds all of the following:

@Configuration tags the class as a source of bean definitions for the application context.

@EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.

Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.

@ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.



2.@EnableAutoConfiguration注解(开启自动配置):@EnableAutoConfiguration注解的作用在于让 Spring Boot 根据应用所声明的依赖来对 Spring 框架进行自动配置,这就减少了开发人员的工作量。

The second class-level annotation is @EnableAutoConfiguration. This annotation tells Spring Boot to “guess” how you will want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration will assume that you are developing a web application and setup Spring accordingly.


Starters and Auto-Configuration
Auto-configuration is designed to work well with “Starters”, but the two concepts are not directly tied. You are free to pick-and-choose jar dependencies outside of the starters and Spring Boot will still do its best to auto-configure your application.

第二个类级别注解是@EnableAutoConfiguration。这个注解告诉Spring Boot“猜测”将如何配置Spring,它是基于添加的jar依赖。 由于spring-boot-starter-web添加了Tomcat和Spring MVC,因此自动配置将假设正在开发一个Web应用程序并相应地设置Spring。
启动器和自动配置(Starters & Auto-Configuration):
自动配置旨在与“Starters”配合使用,但这两个概念不直接绑定。可以自由选择和选择起始者以外的jar依赖,Spring Boot仍将尽力自动配置应用程序。



3.main()方法

The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Did you notice that there wasn’t a single line of XML? No web.xml file either. This web application is 100% pure Java and you didn’t have to deal with configuring any plumbing or infrastructure.

“main”方法应用程序的最后一部分是主(main)方法。 这只是一个遵循Java约定的应用程序入口点的标准方法。main方法通过调用run来委托Spring Boot SpringApplication类。SpringApplication将引导应用程序,启动Spring,从而启动自动配置Tomcat Web服务器。需要传递Example.class作为run方法的参数来告诉SpringApplication,这是主要的Spring组件。args数组也被传递以暴露任何命令行参数。



4.继承 spring-boot-starter-parent

作用是Spring-Boot为我们自动添加相关的依赖,如果不想使用Spring Boot中的默认版本,可以在pom.xml文件的properites标签中指定我们自己想要引入的版本,这样就可以覆盖默认的版本。
比如想使用不同版本的Spring Data,具体如下:


    Fowler-SR2

在properties标签中还可以指定JDK编译的版本和项目的编码格式:


    
    UTF-8
    UTF-8
    
    1.8

5.spring-boot-maven-plugin 插件
可以为SpringBoog项目提供Maven的操作方式,Spring Boot Maven plugin的5个Goals

* spring-boot:repackage,默认goal。在mvn package之后,再次打包可执行的jar/war,同时保留mvn package生成的jar/war为.origin
* spring-boot:run,运行Spring Boot应用
* spring-boot:start,在mvn integration-test阶段,进行Spring Boot应用生命周期的管理
* spring-boot:stop,在mvn integration-test阶段,进行Spring Boot应用生命周期的管理
* spring-boot:build-info,生成Actuator使用的构建信息文件build-info.properties

这里介绍一下 spring-boot:repackage 和 spring-boot:run 这两个。
spring-boot:repackage :可以将项目打包成fat jar(executable jar)后,命令是:
mvm package spring-boot:repackage,或者直接使用 mvm package,(如果使用 mvn spring-boot:repackage 则会报错)。之后我们就可以直接 通过 java -jar 的命令来启动这个 SpringBoot 项目(试了一下即使没有配置 mainClass 也是成功的)

mvn spring-boot:run:在 pom.xml 文件所在的目录下运行该命令即可启动 SpringBoot 项目,和在 Application.java 中运行main()方法的结果是一样的

The Spring Boot Maven plugin provides many convenient features:

It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service.

It searches for the public static void main() method to flag as a runnable class.

It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.

本文示例代码已上传到github: https://github.com/liaosilzu2...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76381.html

相关文章

  • Spring Boot》开发个“HelloWorld”的 web 应用

    摘要:一概括,如果使用开发一个的应用创建一个项目并且导入相关包。创建一个编写一个控制类需要一个部署应用的服务器如,特点设计目的是用来简化新应用的初始搭建以及开发过程。启动器可以和位于同一个包下,或者位于的上一级包中,但是不能放到的平级以及子包下。 一,Spring Boot 介绍 Spring Boot不是一个新的框架,默认配置了多种框架使用方式,使用SpringBoot很容易创建一个独立运...

    chaosx110 评论0 收藏0
  • Spring Boot 2.x():HelloWorld

    摘要:简介本系列基于的官方文档,除去了文档中一些冗余的东西,加上了一些自己的理解,意图是在于帮助更多初识的人来进行一次探险。本系列建议具有基础和使用经验的同学学习。至此,一个程序就编写完毕了。 简介 本系列基于Spring Boot 2.x 的官方文档,除去了文档中一些冗余的东西,加上了一些自己的理解,意图是在于帮助更多初识Spring Boot的人来进行一次探险。 本系列建议具有Java基...

    xuxueli 评论0 收藏0
  • Spring BootSpring Boot——HelloWorld

    摘要:使用嵌入式容器,应用无需达成包。自动依赖与版本控制。准生产环境的运行时应用监控。告诉开启自动配置功能,这样自动配置才能生效。其组成为为的底层注解,表明给容器中导入一个组件,导入的组建由类提供。 Spring Boot——入门 spring boot简化了spring的开发,是J2EE一站式解决方案。 Spring Boot 的优缺点 优点 快速创建独立运行的服务,与主流框架集成。 使...

    hellowoody 评论0 收藏0
  • 从0搭建SpringBootHelloWorld -- Java版本

    摘要:前言以前总是利用创建工程来使用只知其然不知其所以然今天从搭建一个基于的的项目创建工程与安装依赖利用或等创建一个工程一路即可此时的目录结构如下修改安装首先在中加入继承的主程序和一些依赖然后的加入程序依赖使成为项目框架主程序 前言 以前总是利用start.spring.io创建spring-boot工程来使用 ,只知其然不知其所以然 今天从0搭建一个基于mvnen的spring-boot...

    kaka 评论0 收藏0
  • springboot学习()——helloworld

    摘要:关于的自动配置,这个是重点之一,后面细说。在后续的学习中会慢慢学习到。红色标记的就是已经扫描到了并初始化成功了。 以下内容,如有问题,烦请指出,谢谢 springboot出来也很久了,以前零散地学习了不少,不过很长时间了都没有在实际中使用过了,忘了不少,因此要最近准备抽时间系统的学习积累下springboot,给自己留个根。 因为以前学过一些,这里就主要根据官方文档来学习了,可能会根据...

    The question 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<