前序:在Spring Boot出现之前,Spring项目会存在多个配置文件,比如web.xml,像SSM项目,会有很多xml.SpringBoot的出现就是简化这些xml,现在只需要配置一个application.properties,就会移除我们以前的很多烦恼,作为一个pring Boot 并不是用来替代 Spring 的解决方案,而是和 Spring 框架紧密结合用于提升 Spring 开发者体验的工具。同时它集成了大量常用的第三方库配置,Spring Boot应用中这些第三方库几乎可以是零配置的开箱即用(out-of-the-box),大部分的 Spring Boot 应用都只需要非常少量的配置代码(基于 Java 的配置),开发者能够更加专注于业务逻辑。
现在SpringBoot已经研发到2.0,我们就创建一个2.0项目,一旦上手,开发非常快,SpringBoot是基于maven开发,有很多第三方查价。SpringBoot提供了一系列Starter,你可以自己开发自己的Starter然后导进去自己的Maven仓库
名称 | 作用 |
---|---|
spring-boot-starter-web | Web开发支持,自带tomcat |
spring-boot-starter-aop | Aop开发支持,使用AspectJ |
spring-boot-starter-jdbc | Spring JDBC |
spring-boot-starter-data-jpa | JPA方式访问数据库,使用HIbernate作为JPA实现 |
spring-boot-starter-data-elasticsearch | 集成Elasticsearch,默认访问localhost:9200 |
spring-boot-starter-data-data-redis | 集成Redis,使用JRedis,默认连接localhost:6379 |
spring-boot-starter-cache | 缓存,支持多种缓存方式,如本地、redis、Ehcache等 |
spring-boot-devtools | 应用程序快速重启的工具,提升开发体验 |
spring-boot-starter-data-mongodb | 集成MongoDB,默认访问mongodb://localhost/test |
spring-boot-starter-data-neo4j | 集成neo4j,默认访问localhost:7474 |
spring-boot-starter-data-gemfire | 集成分布式缓存 |
spring-boot-starter-data-solr | 基于Apache lucene的搜索平台,默认访问http://localhost:8983/solr |
spring-boot-starter-data-cassandra | 集成neo4j,默认访问localhost:7474 |
spring-boot-starter-data-ldap | 集成Idap |
spring-boot-starter-activemq | 消息集成ActiveMQ支持 |
spring-boot-starter-amqp | 消息集成AMQP协议支持,如支持RabbitMQ |
spring-boot-starter-jta-atomikos | 分布式事务支持,使用atomikos |
spring-boot-starterjta-bitronix | 一个开源的分布式事务支持 |
spring-boot-starter-test | 包含JUnit、SpringTest、Hamcrest、Mockito等测试工具 |
spring-boot-starte-webservices | webservice支持 |
spring-boot-starte-websocket | websocket支持 |
spring-boot-starte-jersey | REST应用和Jersey支持 |
spring-boot-starte-freemarker | Freemaker支持 |
一、hello SpringBoot
开发工具eclipse/idea,听说Idea现在非常火,但是本人用eclipse用的非常久了,用着非常顺手,习惯一个新的开发工具,要熟练掌握很费事。需要Maven、JDK8这些都还没安装,你就去安装吧。
1.1 创建一个Mave工程
不会新建Maven项目的建议学习下Maven
先新创建一个Maven项目new—项目—maven—Maven Project,会创建好一个新的pom文件
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/11868d2450f369339b3c0b50eb84ef4f.png)
导入我们需要的第三方工具
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yuyi</groupId>
<artifactId>springBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springBoot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.3</version>
</dependency>
<!-- springboot websocket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<packaging>war</packaging>
</project>
1.2 Hello SpringBoot
创建一个Application.java,这个类放在最外层,这样启动的时候会全部扫到
package com.yuyi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
创建一个Controller
package com.yuyi.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
* @author lcc
* @data :2018年8月22日 下午3:25:13
*/
/**
@Controller 是返回页面
@RestController 是返回的JSON
*/
@RestController
public class IndexController {
@RequestMapping("login")
public String login(){
return "hello SpringBoot";
}
}
这个时候启动我们刚刚创建的Application.java 访问http://localhost:8080/login ,如图片所示启动成功,要是有其他错误,可以留言访问
好了一个基本的SpringBoot项目创建成功