Spring Boot 具有如下特性:

  1. 为基于 Spring 的开发提供更快的入门体验

  2. 开箱即用,没有代码生成,也无需 XML 配置。同时也可以修改默认值来满足特定的需求。

  3. 提供了一些大型项目中常见的非功能性特性,如嵌入式服务器、安全、指标,健康检测、外部配置等。

  4. Spring Boot 并不是不对 Spring 功能上的增强,而是提供了一种快速使用 Spring 的方式。

快速入门案例:

最终pom.xml:

<?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">
<!--自己项目本身所属的父工程-->
<parent>
<artifactId>project_demo</artifactId>
<groupId>com.zy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>springboot_demo</artifactId> <!--变更jdk版本-->
<properties>
<java.version>1.7</java.version>
</properties> <!--自己项目本身是有parent的情况-->
<!--type 是 pom scope 是 import 这种类型的dependency只能在 dependencyManagement 标签中声明-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<!--spring boot起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!--热部署 在idea中需要设置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency> <!--activemq 消息中间件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
</dependencies>
</project>

首先是我们的pom.xml文件:

1,官方示例中,让我们继承一个spring的 spring-boot-starter-parent 这个parent:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

2,但是,一般情况下,在我们自己的项目中,会定义一下自己的 parent 项目,这种情况下,上面的这种做法就行不通了。那么,该如何来做呢?其实,在spring的官网也给出了变通的方法的:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

3,然后,把我们项目中的 子项目 中,parent 的声明,修改为我们自己项目的 parent 项目就可以了

<?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">
<!--自己项目本身所属的父工程-->
<parent>
<artifactId>project_demo</artifactId>
<groupId>com.zy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>springboot_demo</artifactId> <!--变更jdk版本-->
<properties>
<java.version>1.7</java.version>
</properties> <!--自己项目本身是有parent的情况-->
<!--type 是 pom scope 是 import 这种类型的dependency只能在 dependencyManagement 标签中声明-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<!--spring boot起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

其他一些配置文件默认在resources下的application.properties文件中:

#修改tomcat端口
server.port=8088
#-------
url=http://www.1688.com
#activemq地址
spring.activemq.broker-url=tcp://192.168.25.128:61616

简单示例 HelloController:

package com.zy.demo;

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("/hello")
public class HelloController { @Autowired
private Environment env; @RequestMapping("/helloSpringBoot")
public String hello() {
return "hello springboot";
} @RequestMapping("/info")
public String info() {
return "info:" + env.getProperty("url");
}
}

我们现在想访问HelloController,所以需要创建一个引导类Application:

package com.zy.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);
}
}

现在就可以访问http://localhost:8088/hello/helloSpringBoot  (由于我们在application.properties配置文件中修改了tomcat的端口为8088)。

SpringBoot整合ActiveMQ:

不需要其他配置,值需要在pom文件中引入ActiveMQ的依赖,并创建生产者 消费者即可:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

生产者Producer:

package com.zy.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.HashMap;
import java.util.Map; @RestController
public class Producer {
//注入jsmtemplate
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate; @RequestMapping("/sendMsg")
public void sendMsg(String msg) {
jmsMessagingTemplate.convertAndSend("my_msg", msg);
System.out.println("msg发送成功");
} @RequestMapping("/sendMap")
public void sendMap() {
Map map = new HashMap();
map.put("mobile", "13888888888");
map.put("content", "王总喜提兰博基尼");
jmsMessagingTemplate.convertAndSend("my_map", map);
System.out.println("map发送成功");
}
}

消费者Consumer:

package com.zy.demo;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component; import java.util.Map; @Component
public class Consumer {
@JmsListener(destination = "my_msg")
public void readMsg(String text) {
System.out.println("接收到消息:" + text);
} @JmsListener(destination = "my_map")
public void readMap(Map map) {
System.out.println(map);
}
}

访问http://localhost:8088/...... 即可。

SpringBoot整合ActiveMQ快速入门的更多相关文章

  1. SpringBoot整合mybatis快速入门

    一.创建一个SpringBoot项目                 二.引入相关依赖 <!--web核心依赖--> <dependency> <groupId>o ...

  2. Springboot 完整搭建快速入门,必看!

    前言 手把手教你Springboot微服务项目搭建快速入门,通过本文学习Springboot的搭建快速入门,掌握微服务大致的配置服务,后续将会继续将核心组件引入到项目中,欢迎关注,点赞,转发. Spr ...

  3. Web项目容器集成ActiveMQ & SpringBoot整合ActiveMQ

    集成tomcat就是随项目启动而启动tomcat,最简单的方法就是监听器监听容器创建之后以Broker的方式启动ActiveMQ. 1.web项目中Broker启动的方式进行集成 在这里采用Liste ...

  4. SpringBoot系列: RestTemplate 快速入门

    ====================================相关的文章====================================SpringBoot系列: 与Spring R ...

  5. SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...

  6. 解决Springboot整合ActiveMQ发送和接收topic消息的问题

    环境搭建 1.创建maven项目(jar) 2.pom.xml添加依赖 <parent> <groupId>org.springframework.boot</group ...

  7. SpringBoot整合ActiveMQ和开启持久化

    一.点对点 1.提供者目录展示 2.导入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...

  8. ActiveMQ 笔记(四)Spring\SpringBoot 整合 Activemq

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.Spring 整合Activemq 1.所需jar包 <dependencies> &l ...

  9. Springboot整合activemq

    今天呢心血来潮,也有很多以前的学弟问到我关于消息队列的一些问题,有个刚入门,有的有问题都来问我,那么今天来说说如何快速入门mq. 一.首先说下什么是消息队列? 1.消息队列是在消息的传输过程中保存消息 ...

随机推荐

  1. Python 三元条件判断表达式(and or/if else)

    参考: http://wangye.org/blog/archives/690/

  2. pyqtree

    pyqtree module API Documentation Classes class Index The top spatial index to be created by the user ...

  3. Centos7.x破解密码

    Centos7.x破解密码 centos7 破解密码 重置Centos 7 Root密码的方式和Centos 6完全不同.让我来展示一下到底如何操作. 1 .在启动grub菜单,选择编辑选项启动 14 ...

  4. stm32f0系列在SWD模式下载时复位失败

    用stm32f030K6T6做了个小玩意,仿真电路就直接把3.3V,SWDIO,SWCLK,GND引出来连接到j-link的这四个角上,SWDIO和SWCLK引脚既没有上拉也没有下拉.     MCU ...

  5. 【JS】手机屏幕旋转判断

    function readDeviceOrientation() { if (Math.abs(window.orientation) === 90) { // Landscape alert('横屏 ...

  6. 【UVa】1343 The Rotation Game(IDA*)

    题目 题目     分析 lrj代码.... 还有is_final是保留字,害的我CE了好几发.     代码 #include <cstdio> #include <algorit ...

  7. 【洛谷】P1892 团伙(并查集)+ 求助

    题目描述 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友. 两个强盗是同一团伙的 ...

  8. iconv 解决utf-8和gb2312编码转换问题

    $content = iconv("utf-8","gb2312//IGNORE",$content); //utf-8转gbk $content = icon ...

  9. 如何实现session共享

    http://www.cnblogs.com/xiehuiqi220/p/3592300.html 首先我们应该明白,为什么要实现共享,如果你的网站是存放在一个机器上,那么是不存在这个问题的,因为会话 ...

  10. Confluence 5.4.2安装

    默认安装路径 /opt/atlassian/confluence/bin Confluence是Atlassian公司出品的团队协同与知识管理工具. Confluence是一个专业的企业知识管理与协同 ...