Spring boot简单入门学习

     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部分引用去掉,目前用不到,后续连接数据库的时候会用到)

<?xml version="1.0" encoding="UTF-8"?>
4.0.0com.zxlsprintboot-examples1.0-SNAPSHOT
    
    org.springframework.bootspring-boot-starter-parent1.5.4.RELEASE
    
    org.springframework.bootspring-boot-starter-data-jpaorg.springframework.bootspring-boot-starter-web
    
    
    
    
    
    
    
    
    
    
    
    org.hibernatehibernate-coreorg.hsqldbhsqldb
    
    org.springframework.bootspring-boot-starter-testtestmysqlmysql-connector-java
    
    org.springframework.bootspring-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相同@ComponentScan
public 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.
 */
@RestController
public 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
Name Description Pom
spring-boot-starter Core starter, including auto-configuration support, logging and YAML Pom
spring-boot-starter-activemq Starter for JMS messaging using Apache ActiveMQ Pom
spring-boot-starter-amqp Starter for using Spring AMQP and Rabbit MQ Pom
spring-boot-starter-aop Starter for aspect-oriented programming with Spring AOP and AspectJ Pom
spring-boot-starter-artemis Starter for JMS messaging using Apache Artemis Pom
spring-boot-starter-batch Starter for using Spring Batch Pom
spring-boot-starter-cache Starter for using Spring Framework’s caching support Pom
spring-boot-starter-cloud-connectors Starter for using Spring Cloud Connectors which simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku Pom
spring-boot-starter-data-cassandra Starter for using Cassandra distributed database and Spring Data Cassandra Pom
spring-boot-starter-data-couchbase Starter for using Couchbase document-oriented database and Spring Data Couchbase Pom
spring-boot-starter-data-elasticsearch Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch Pom
spring-boot-starter-data-gemfire Starter for using GemFire distributed data store and Spring Data GemFire Pom
spring-boot-starter-data-jpa Starter for using Spring Data JPA with Hibernate Pom
spring-boot-starter-data-ldap Starter for using Spring Data LDAP Pom
spring-boot-starter-data-mongodb Starter for using MongoDB document-oriented database and Spring Data MongoDB Pom
spring-boot-starter-data-neo4j Starter for using Neo4j graph database and Spring Data Neo4j Pom
spring-boot-starter-data-redis Starter for using Redis key-value data store with Spring Data Redis and the Jedis client Pom
spring-boot-starter-data-rest Starter for exposing Spring Data repositories over REST using Spring Data REST Pom
spring-boot-starter-data-solr Starter for using the Apache Solr search platform with Spring Data Solr Pom
spring-boot-starter-freemarker Starter for building MVC web applications using FreeMarker views Pom
spring-boot-starter-groovy-templates Starter for building MVC web applications using Groovy Templates views Pom
spring-boot-starter-hateoas Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS Pom
spring-boot-starter-integration Starter for using Spring Integration Pom
spring-boot-starter-jdbc Starter for using JDBC with the Tomcat JDBC connection pool Pom
spring-boot-starter-jersey Starter for building RESTful web applications using JAX-RS and Jersey. An alternative tospring-boot-starter-web Pom
spring-boot-starter-jooq Starter for using jOOQ to access SQL databases. An alternative tospring-boot-starter-data-jpa or spring-boot-starter-jdbc Pom
spring-boot-starter-jta-atomikos Starter for JTA transactions using Atomikos Pom
spring-boot-starter-jta-bitronix Starter for JTA transactions using Bitronix Pom
spring-boot-starter-jta-narayana Spring Boot Narayana JTA Starter Pom
spring-boot-starter-mail Starter for using Java Mail and Spring Framework’s email sending support Pom
spring-boot-starter-mobile Starter for building web applications using Spring Mobile Pom
spring-boot-starter-mustache Starter for building MVC web applications using Mustache views Pom
spring-boot-starter-security Starter for using Spring Security Pom
spring-boot-starter-social-facebook Starter for using Spring Social Facebook Pom
spring-boot-starter-social-linkedin Stater for using Spring Social LinkedIn Pom
spring-boot-starter-social-twitter Starter for using Spring Social Twitter Pom
spring-boot-starter-test Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito Pom
spring-boot-starter-thymeleaf Starter for building MVC web applications using Thymeleaf views Pom
spring-boot-starter-validation Starter for using Java Bean Validation with Hibernate Validator Pom
spring-boot-starter-web Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container Pom
spring-boot-starter-web-services Starter for using Spring Web Services Pom
spring-boot-starter-websocket Starter for building WebSocket applications using Spring Framework’s WebSocket support Pom

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

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
Name Description Pom
spring-boot-starter-jetty Starter for using Jetty as the embedded servlet container. An alternative to spring-boot-starter-tomcat Pom
spring-boot-starter-log4j2 Starter for using Log4j2 for logging. An alternative to spring-boot-starter-logging Pom
spring-boot-starter-logging Starter for logging using Logback. Default logging starter Pom
spring-boot-starter-tomcat Starter for using Tomcat as the embedded servlet container. Default servlet container starter used byspring-boot-starter-web Pom
spring-boot-starter-undertow Starter for using Undertow as the embedded servlet container. An alternative to spring-boot-starter-tomcat Pom



猜你喜欢

转载自blog.csdn.net/long290046464/article/details/76039578