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
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);
}
使用根包也允许使用@ComponentScan注释,而不需要指定basePackage属性。@SpringBootApplication如果主类在根包中,也可以使用 注释
该@SpringBootApplication注解相当于使用@Configuration, @EnableAutoConfiguration并@ComponentScan与他们的默认属性
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;
}
}
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 |
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 |
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 |