1.1.1 起源
springBoot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。用我的话来理解,就是 Spring Boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像 Maven 整合了所有的 Jar 包,Spring Boot 整合了所有的框架。
1.1.2 Spring Boot 具有如下特性:
(1)为基于 Spring 的开发提供更快的入门体验
(2)开箱即用,没有代码生成,也无需 XML 配置。同时也可以修改默认值来满足特定的需求。
(3)提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标,健康检测、外部配置等。
(4)Spring Boot 并不是不对 Spring 功能上的增强,而是提供了一种快速使用 Spring 的方式。
1.1.3 SpringBoot入门小demo
创建Maven工程 springboot_demo
1.在pom.xml添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2.变更JDK版本
我们发现默认情况下工程的JDK版本是1.6,而我们通常使用的是1.7的版本,所有需要在pom.xml中添加以下配置
<properties>
<java.version>1.7</java.version>
</properties>
3.创建一个引导类
package com.mufeng.demo;
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);
}
}
简单解释一下:@SpringBootApplication其实就是以下三个注解的总和
@Configuration:用于定义一个配置类
@EnableAutoCofiguration:springBoot 会自动根据你的jar包的依赖自动配置项目
@ComponentScan:根据定义的扫描路径,把符合扫描规则的类装配到spring容器中,
4.使用SpringMVC实现hello Word输出
**注:**按照原来的做法,需要在web.xml添加一个DispatcherServlet的配置,再添加一个spring的配置文件。但是我们使用SpringBoot,这一切都可以省略。我们直接开始写Controller类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
@RequestMapping("/hello")
public String hello(){
return "hello word";
}
}
1.1.4 修改Tomcat启动端口
在src/main/resources下创建application.properties
server.prot=8080
1.1.5 读取配置文件信息
在src/main/resources下的application.properties 增加配置
url=http://localhost:8083/test/hello
在类中读取这个配置的信息,修改Controller类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private Environment environment;
@RequestMapping("/hello")
public String hello(){
return "hello word"+ environment.getProperty("url");
}
}