虽然 Spring 的组件代码是轻量级的,但它的配置却是重量级的。一开始,Spring 用 XML 配置,而且是很多 XML 配置。Spring 2.5 引入了基于注解的组件扫描,这消除了大量针对应用程序自身组件的显式 XML 配置。Spring 3.0 引入了基于 Java 的配置,这是一种类型安全的可重构配置方式,可以代替 XML,所有这些配置都代表了开发时的损耗,因为在思考Spring 特性配置和解决业务问题之间需要进行思维切换,所以写配置挤占了写应用程序逻辑的时间。和所有框架一样,Spring 实用,但与此同时它要求的回报也不少
除此之外,项目的依赖管理也是件吃力不讨好的事情。决定项目里要用哪些库就已经够让人头痛的了,你还要知道这些库的哪个版本和其他库不会有冲突,这难题实在太棘手。并且,依赖管理也是一种损耗,添加依赖不是写应用程序代码。一旦选错了依赖的版本,随之而来的不兼容问题毫无疑问会是生产力杀手,Spring Boot 让这一切成为了过去
1. 什么是Spring-Boot
Spring Boot
是 Spring
社区较新的一个项目。该项目的目的是帮助开发者更容易的创建基于 Spring
的应用程序和服务,让更多人的人更快的对 Spring 进行入门体验
,为 Spring
生态系统提供了一种固定的、约定优于配置风格的框架
Spring-Boot特性
- 为基于 Spring 的开发提供更快的入门体验
- 开箱即用,没有代码生成,也无需 XML 配置。同时也可以修改默认值来满足特定的需求
- 提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标,健康检测、外部配置等
- Spring Boot 并不是不对 Spring 功能上的增强,而是提供了一种快速使用Spring 的方式
2. Spring-Boot 入门案例
- Maven-pom文件导入坐标
<?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>
<groupId>com.pyg</groupId>
<artifactId>springBoot-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 导入依赖,使Maven项目变成springBoot项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<!-- 导入springBoot开发web项目依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
- 创建配置文件 application.properties 不用写任何内容
- 创建启动类 Application
package com.pyg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @ Description
* @ auther 宁宁小可爱
* @ create 2020-02-06 15:04
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
// springBoot项目入口是 main函数
/*
* 上线只需要执行 java -jar XXX.jar 即可
* 启动: 自动启动内置的tomcat服务器
* 注意: 此启动类必须在controller类上层目录
* */
SpringApplication.run(Application.class,args);
}
}
- 创建controller,展示Hello World
package com.pyg.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ Description
* @ auther 宁宁小可爱
* @ create 2020-02-06 15:06
*/
@RestController
@RequestMapping("/app")
public class AppController {
/*
* springBoot的入门
* */
@RequestMapping("/hello")
public String hello(){
return "Hello World Spring-Boot";
}
}
- 项目目录结构
- 启动成功
- 访问 http://localhost:8080/app/hello
3. Spring-Boot 修改端口
- 修改启动端口只需要在 application.properties 文件中添加配置即可
server.port=80
- 修改后在启动即可看到已经修改成功
- 访问刚刚的hello项目,修改成功
4. Spring-Boot 读取自定义配置
- application配置文件中添加自定义配置
server.port=80
# 自定义配置
url = www.baidu.com
- controller中导入环境,获取配置文件内容
package com.pyg.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;
import java.util.HashMap;
import java.util.Map;
/**
* @ Description
* @ auther 宁宁小可爱
* @ create 2020-02-06 15:06
*/
@RestController
@RequestMapping("/app")
public class AppController {
/*
* springBoot读取自定义配置
* 引入org.springframework.core.env.Environment
* */
@Autowired
private Environment env;
@RequestMapping("/hello")
public Map hello(){
Map map = new HashMap();
map.put("name","宁宁");
map.put("address","中国");
// 获取自定义配置的内容
map.put("url",env.getProperty("url"));
return map;
}
}
- 结果展示
5. Spring-Boot 热部署
我们在开发中反复修改类、页面等资源,每次修改后都是需要重新启动才生效,这样每次启动都很麻烦,浪费了大量的时间,能不能在我修改代码后不重启就能生效呢?可以,在pom.xml 中添加如下配置就可以实现这样的功能,我们称之为热部署
- pom文件导入坐标依赖就可以了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
6. Spring-Boot 整合ActiveMQ
- pom文件中导入ActiveMQ的坐标依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
- 自定义ActiveMQ服务地址,在application.properties配置文件中配置
server.port=80
# 配置mq服务地址
spring.activemq.broker-url=tcp://192.168.25.120:61616
6.1 使用整合后的ActiveMQ发送消息
- controller中代码
package com.pyg.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ Description
* @ auther 宁宁小可爱
* @ create 2020-02-06 15:06
*/
@RestController
@RequestMapping("/app")
public class AppController {
/*
* springBoot整合ActiveMQ发送消息
* 引入org.springframework.jms.core.JmsTemplate
* 这里直接引入即可,springboot内置了此服务
* */
@Autowired
private JmsTemplate jmsTemplate;
@RequestMapping("/send/{message}")
public String sendMessage(@PathVariable String message){
jmsTemplate.convertAndSend("spring-MQ",message);
return "OK";
}
}
- 输入路径访问,结果展示发送成功
- 查看是否发送成功,可以看到已经发送成功
6.2 使用整合后的ActiveMQ接收消息
- controller中代码
package com.pyg.controller;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Controller;
/**
* @ Description
* @ auther 宁宁小可爱
* @ create 2020-02-06 16:10
*/
@Controller
public class RecvController {
/*
* 需求: 接收MQ消息
* 消息空间: spring-MQ
* 只需要加注解,注解中标明消息空间即可自动接收
* */
@JmsListener(destination = "spring-MQ")
public void recvMessage(String message){
System.out.println("接收的消息为: " +message);
}
}
- 可以看到接收成功