Spring boot简单入门学习

来源:互联网 发布:延边大学知乎 编辑:程序博客网 时间:2024/05/29 11:05

     Spring boot简单入门学习


         最近公司所有的项目架构都升级了,思想采用了微服务的思想,技术架构采用了spring cloud,虽然开始了边学边用的阶段,以及踩到了不少的坑,但是里边的原理以及一些高级应用还是不清楚,而正要进一步学习spring cloud的时候发现了spring cloud是基于spring boot的,于是乎又转头去了解了下spring boot是个什么东东。

1.spring boot简介-->粘贴自 官网

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

You can use Spring Boot to create 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”.

Our primary goals are:

Provide a radically faster and widely accessible getting started experience for all Spring development.
Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.
Provide a range of non-functional features that are common to large classes of projects (e.g. embedded servers, security, metrics, health checks, externalized configuration).
Absolutely no code generation and no requirement for XML configuration.

大意如下:

Spring Boot可以轻松创建可以“运行”的独立的,生产级的基于Spring的应用程序。我们对Spring平台和第三方图书馆有一个看法,所以你可以从最开始的时候开始。大多数Spring Boot应用程序需要很少的Spring配置。

您可以使用Spring Boot创建可以使用java -jar 或更多传统战争部署的Java应用程序。我们还提供一个运行“spring脚本”的命令行工具。

我们的主要目标是:

为所有的Spring开发提供一个更快,更广泛的入门体验。
被开除箱子的意见,但随着需求开始偏离默认值而迅速脱离出来。
提供大量项目(例如嵌入式服务器,安全性,指标,健康检查,外部化配置)通用的一系列非功能特性。
绝对没有代码生成,也不需要XML配置


了解了spring boot大致是干嘛的了,那么我们可以做一个简单的小例子来体验了一把了,就做一个简单的controller访问的helloword


2.创建一个简单的helloword

整体项目的目录结构如下图所示:

2.1  创建一个maven项目,pom.xml如下所示(此部分可将jpa与hibernate部分引用去掉,目前用不到,后续连接数据库的时候会用到)

    4.0.0    com.zxl    sprintboot-examples    1.0-SNAPSHOT                org.springframework.boot        spring-boot-starter-parent        1.5.4.RELEASE                                org.springframework.boot            spring-boot-starter-data-jpa                            org.springframework.boot            spring-boot-starter-web                                                                                        org.hibernate            hibernate-core                            org.hsqldb            hsqldb                                    org.springframework.boot            spring-boot-starter-test            test                            mysql            mysql-connector-java                                                        org.springframework.boot                spring-boot-maven-plugin                        

2.2  创建配置文件
此处的配置文件采用YAML语法的yml格式的文件替代了properties格式的文件,若想了解YAML语法还请度娘下(本人也是去度娘了解的),文件名:application.yml,文件内容为server.port=8089,当然了也不可不创建此配置文件,若不创建则启动项目后会默认分配 8080端口

2.3  创建程序的启动java文件
package com.zxl.examples;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication ////与@Configuration @EnableAutoConfiguration相同@ComponentScanpublic class MyApplication {    public static void main(String[] args) throws Exception {        SpringApplication.run(MyApplication.class, args);    }
说明:我们通常建议您将主应用程序类定位到其他类之上的根包中。该@EnableAutoConfiguration注解往往放在你的主类,它隐含地定义为某些项目一基地“搜索包”。例如,如果您正在编写JPA应用程序,@EnableAutoConfiguration则将使用注释类的包 来搜索@Entity项目
使用根包也允许使用@ComponentScan注释,而不需要指定basePackage属性。@SpringBootApplication如果主类在根包中,也可以使用 注释
该@SpringBootApplication注解相当于使用@Configuration, @EnableAutoConfiguration并@ComponentScan与他们的默认属性

2.4  创建一个controller
package com.zxl.examples.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;/** * Created by Administrator on 2017/7/19. */@RestControllerpublic class HelloWorldController {    @GetMapping("/hello/{name}")    public String hello(@PathVariable String name){        return "hello "+name;    }}
2.5  启动项目并访问
打开启动文件,右键run->main方法,然后在浏览器里边敲入路径访问,如下图
到这里spring boot的简单入门就成功了,如要想要了解更多还请在网上多搜搜他人的博客或者直接去官网上学习。
官网文档地址:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-documentation-about
官网git源码例子:https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples



小提示:
1.spring boot自带嵌入的tomcat,jetty等容器,所以不用额外配置容器
2.当把项目打成可执行的jar后可采用命令模式启动,如:java -jar xcloud-print-service-1.0.jar --spring.profiles.active=dev
3.spring boot启动时默认会加载bootstrap.yml配置文件,若没有则加载application.yml文件,即:bootstrap.yml为全局配置文件,为最高级别

扩展介绍:

Table 13.1. Spring Boot application starters
NameDescriptionPomspring-boot-starterCore starter, including auto-configuration support, logging and YAMLPomspring-boot-starter-activemqStarter for JMS messaging using Apache ActiveMQPomspring-boot-starter-amqpStarter for using Spring AMQP and Rabbit MQPomspring-boot-starter-aopStarter for aspect-oriented programming with Spring AOP and AspectJPomspring-boot-starter-artemisStarter for JMS messaging using Apache ArtemisPomspring-boot-starter-batchStarter for using Spring BatchPomspring-boot-starter-cacheStarter for using Spring Framework’s caching supportPomspring-boot-starter-cloud-connectorsStarter for using Spring Cloud Connectors which simplifies connecting to services in cloud platforms like Cloud Foundry and HerokuPomspring-boot-starter-data-cassandraStarter for using Cassandra distributed database and Spring Data CassandraPomspring-boot-starter-data-couchbaseStarter for using Couchbase document-oriented database and Spring Data CouchbasePomspring-boot-starter-data-elasticsearchStarter for using Elasticsearch search and analytics engine and Spring Data ElasticsearchPomspring-boot-starter-data-gemfireStarter for using GemFire distributed data store and Spring Data GemFirePomspring-boot-starter-data-jpaStarter for using Spring Data JPA with HibernatePomspring-boot-starter-data-ldapStarter for using Spring Data LDAPPomspring-boot-starter-data-mongodbStarter for using MongoDB document-oriented database and Spring Data MongoDBPomspring-boot-starter-data-neo4jStarter for using Neo4j graph database and Spring Data Neo4jPomspring-boot-starter-data-redisStarter for using Redis key-value data store with Spring Data Redis and the Jedis clientPomspring-boot-starter-data-restStarter for exposing Spring Data repositories over REST using Spring Data RESTPomspring-boot-starter-data-solrStarter for using the Apache Solr search platform with Spring Data SolrPomspring-boot-starter-freemarkerStarter for building MVC web applications using FreeMarker viewsPomspring-boot-starter-groovy-templatesStarter for building MVC web applications using Groovy Templates viewsPomspring-boot-starter-hateoasStarter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOASPomspring-boot-starter-integrationStarter for using Spring IntegrationPomspring-boot-starter-jdbcStarter for using JDBC with the Tomcat JDBC connection poolPomspring-boot-starter-jerseyStarter for building RESTful web applications using JAX-RS and Jersey. An alternative tospring-boot-starter-webPomspring-boot-starter-jooqStarter for using jOOQ to access SQL databases. An alternative tospring-boot-starter-data-jpa or spring-boot-starter-jdbcPomspring-boot-starter-jta-atomikosStarter for JTA transactions using AtomikosPomspring-boot-starter-jta-bitronixStarter for JTA transactions using BitronixPomspring-boot-starter-jta-narayanaSpring Boot Narayana JTA StarterPomspring-boot-starter-mailStarter for using Java Mail and Spring Framework’s email sending supportPomspring-boot-starter-mobileStarter for building web applications using Spring MobilePomspring-boot-starter-mustacheStarter for building MVC web applications using Mustache viewsPomspring-boot-starter-securityStarter for using Spring SecurityPomspring-boot-starter-social-facebookStarter for using Spring Social FacebookPomspring-boot-starter-social-linkedinStater for using Spring Social LinkedInPomspring-boot-starter-social-twitterStarter for using Spring Social TwitterPomspring-boot-starter-testStarter for testing Spring Boot applications with libraries including JUnit, Hamcrest and MockitoPomspring-boot-starter-thymeleafStarter for building MVC web applications using Thymeleaf viewsPomspring-boot-starter-validationStarter for using Java Bean Validation with Hibernate ValidatorPomspring-boot-starter-webStarter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded containerPomspring-boot-starter-web-servicesStarter for using Spring Web ServicesPomspring-boot-starter-websocketStarter for building WebSocket applications using Spring Framework’s WebSocket supportPom

In addition to the application starters, the following starters can be used to add production ready features:
Table 13.2. Spring Boot production starters
NameDescriptionPomspring-boot-starter-actuatorStarter for using Spring Boot’s Actuator which provides production ready features to help you monitor and manage your applicationPomspring-boot-starter-remote-shellStarter for using the CRaSH remote shell to monitor and manage your application over SSH. Deprecated since 1.5Pom

Finally, Spring Boot also includes some starters that can be used if you want to exclude or swap specific technical facets:
Table 13.3. Spring Boot technical starters
NameDescriptionPomspring-boot-starter-jettyStarter for using Jetty as the embedded servlet container. An alternative to spring-boot-starter-tomcatPomspring-boot-starter-log4j2Starter for using Log4j2 for logging. An alternative to spring-boot-starter-loggingPomspring-boot-starter-loggingStarter for logging using Logback. Default logging starterPomspring-boot-starter-tomcatStarter for using Tomcat as the embedded servlet container. Default servlet container starter used byspring-boot-starter-webPomspring-boot-starter-undertowStarter for using Undertow as the embedded servlet container. An alternative to spring-boot-starter-tomcatPom



原创粉丝点击