spring boot注释

1.简介

在以前的文章中,我们学习了如何使用Spring JMS配置项目。 如果查看有关使用Spring JMS进行消息传递的文章介绍 ,您会注意到它是使用XML配置的。 本文将利用Spring 4.1版本中引入的改进 ,并仅使用Java config配置JMS项目。

在这个示例中,我们还将看到使用Spring Boot配置项目是多么容易。

在开始之前,请注意,您可以像往常一样查看下面示例中使用的项目的源代码。

请参阅github上的示例项目 。

栏目:

  1. 介绍。
  2. 示例应用程序。
  3. 设置项目。
  4. 一个使用JMS侦听器的简单示例。
  5. 使用@SendTo将响应发送到另一个队列。
  6. 结论。

2.示例应用程序

该应用程序使用客户端服务将订单发送到JMS队列,在该队列中将注册JMS侦听器并处理这些订单。 收到后,侦听器将通过Store服务存储订单:

我们将使用Order类创建订单:

public class Order implements Serializable {private static final long serialVersionUID = -797586847427389162L;private final String id;public Order(String id) {this.id = id;}public String getId() {return id;}
}

在继续第一个示例之前,我们将首先探讨如何构建项目结构。

3.设置项目

3.1配置pom.xml

首先要做的是将工件spring-boot-starter-parent定义为我们的父pom。

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.2.3.RELEASE</version><relativePath/> <!-- lookup parent from repository -->
</parent>

这个父级基本上设置了几个Maven默认值,并为我们将要使用的主要依赖项提供了依赖项管理,例如Spring版本(4.1.6)。

重要的是要注意,此父pom定义了许多库的版本,但未对我们的项目添加任何依赖关系。 因此,不必担心会得到不使用的库。

下一步是设置Spring Boot的基本依赖关系:

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

除了核心Spring库之外,此依赖项还将带来Spring Boot的自动配置功能。 这将允许框架尝试根据您添加的依赖项自动设置配置。

最后,我们将添加Spring JMS依赖项和ActiveMQ消息代理,将整个pom.xml保留如下:

<groupId>xpadro.spring</groupId>
<artifactId>jms-boot-javaconfig</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>JMS Spring Boot Javaconfig</name><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.2.3.RELEASE</version><relativePath/> <!-- lookup parent from repository -->
</parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><start-class>xpadro.spring.jms.JmsJavaconfigApplication</start-class><java.version>1.8</java.version><amq.version>5.4.2</amq.version>
</properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-core</artifactId><version>${amq.version}</version></dependency>
</dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins>
</build>

3.2使用Java Config进行Spring配置

配置类仅需要配置嵌入式消息代理。 其余的由Spring Boot自动配置:

@SpringBootApplication
public class JmsJavaconfigApplication {private static final String JMS_BROKER_URL = "vm://embedded?broker.persistent=false,useShutdownHook=false";@Beanpublic ConnectionFactory connectionFactory() {return new ActiveMQConnectionFactory(JMS_BROKER_URL);}public static void main(String[] args) {SpringApplication.run(JmsJavaconfigApplication.class, args);}
}

我们使用@SpringBootApplication而不是通常的@Configuration批注。 这个Spring Boot注释也用@Configuration注释。 此外,它还设置其他配置,例如Spring Boot自动配置:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication {

现在都设置好了。 在下一节的示例中,我们将了解如何配置JMS侦听器,因为它已配置了注释。

4.使用JMS侦听器的简单示例

4.1将订单发送到JMS队列

ClientService类负责将新订单发送到JMS队列。 为了做到这一点,它使用一个JmsTemplate:

@Service
public class ClientServiceImpl implements ClientService {private static final String SIMPLE_QUEUE = "simple.queue";private final JmsTemplate jmsTemplate;@Autowiredpublic ClientServiceImpl(JmsTemplate jmsTemplate) {this.jmsTemplate = jmsTemplate;}@Overridepublic void addOrder(Order order) {jmsTemplate.convertAndSend(SIMPLE_QUEUE, order);}
}

在这里,我们使用JmsTemplate转换Order实例并将其发送到JMS队列。 如果您希望直接通过发送消息发送消息,则可以使用新的JmsMessagingTemplate 。 这是更好的选择,因为它使用了更加标准化的Message类。

4.2接收发送到JMS队列的订单

将JMS侦听器注册到JMS侦听器容器就像将@JmsListener批注添加到我们要使用的方法一样简单。 这将在幕后创建一个JMS侦听器容器,该容器将接收发送到指定队列的消息并将它们委派给我们的侦听器类:

@Component
public class SimpleListener {private final StoreService storeService;@Autowiredpublic SimpleListener(StoreService storeService) {this.storeService = storeService;}@JmsListener(destination = "simple.queue")public void receiveOrder(Order order) {storeService.registerOrder(order);}
}

StoreService接收订单并将其保存到已接收订单的列表中:

@Service
public class StoreServiceImpl implements StoreService {private final List<Order> receivedOrders = new ArrayList<>();@Overridepublic void registerOrder(Order order) {this.receivedOrders.add(order);}@Overridepublic Optional<Order> getReceivedOrder(String id) {return receivedOrders.stream().filter(o -> o.getId().equals(id)).findFirst();}
}

4.3测试应用程序

现在让我们添加一个测试来检查我们是否正确完成了所有操作:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = JmsJavaconfigApplication.class)
public class SimpleListenerTest {@Autowiredprivate ClientService clientService;@Autowiredprivate StoreService storeService;@Testpublic void sendSimpleMessage() {clientService.addOrder(new Order("order1"));Optional<Order> storedOrder = storeService.getReceivedOrder("order1");Assert.assertTrue(storedOrder.isPresent());Assert.assertEquals("order1", storedOrder.get().getId());}
}

5.使用@SendTo将响应发送到另一个队列

Spring JMS的另一个附加功能是@SendTo批注。 此批注允许侦听器将消息发送到另一个队列。 例如,以下侦听器从“ in.queue”接收命令,并在存储该命令后将确认发送到“ out.queue”。

@JmsListener(destination = "in.queue")
@SendTo("out.queue")
public String receiveOrder(Order order) {storeService.registerOrder(order);return order.getId();
}

在那里,我们注册了另一个侦听器,它将处理此确认ID:

@JmsListener(destination = "out.queue")
public void receiveOrder(String orderId) {registerService.registerOrderId(orderId);
}

六,结论

有了注释支持,现在可以更轻松地配置Spring JMS应用程序,从而利用带注释的JMS侦听器进行异步消息检索。

翻译自: https://www.javacodegeeks.com/2015/04/configure-a-spring-jms-application-with-spring-boot-and-annotation-support.html

spring boot注释

spring boot注释_使用Spring Boot和注释支持配置Spring JMS应用程序相关推荐

  1. 使用Spring Boot和注释支持配置Spring JMS应用程序

    1.简介 在以前的文章中,我们学习了如何使用Spring JMS配置项目. 如果查看有关使用Spring JMS进行消息传递的文章介绍 ,您会注意到它是使用XML配置的. 本文将利用Spring 4. ...

  2. java如何快速取消注释_关于Java:Eclipse注释/取消注释快捷方式?

    我认为这很容易实现,但是到目前为止,我还没有在Java class editor和jsf faceted webapp XHTML file editor上找到注释/取消注释快捷方式的解决方案: 快速 ...

  3. java编写字符串连接程序注释_一种利用JAVA注释支持多行字符串的方法

    从BeetlSql项目将SQL全放在Beetl模板里得到启发,又想到一个比较偏门的用法.以下代码实测通过,详见jSqlBox项目的test\examples\multipleLineSQL\SqlTe ...

  4. python注释_不建议使用Java注释的正确方法

    python注释 几乎没有什么@Deprecated没有适当的文档看到@Deprecated方法更令人生气的了. 我感到失落. 我应该仍然使用该方法吗? 可能这不是开发人员的意图,这就是为什么他/她添 ...

  5. ctrl+/加注释,去注释_关于以下内容的注释:2014年Google I / O上的“绩效文化”

    ctrl+/加注释,去注释 At Google I/O 2014, Lara Swanson and Paul Lewis discussed performance culture. Since i ...

  6. java添加文档注释_添加Java文档注释

    一.在Eclipse中add javadoc comment的快捷键为: 快捷键为:ALT + SHIFT +J 二.Window-->Preferences-->General--> ...

  7. 去java文件 注释_去除java文件中注释部分

    Alion91:import java.io.File;|@||@|public class DirectoryUtil {|@||@|      /**|@|       * @param args ...

  8. [Spring实战系列](6)配置Spring IOC容器的Bean

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SunnyYoona/article/details/50619900 1. 简介 Spring提供了 ...

  9. Spring MVC 学习笔记 对locale和theme的支持

    Spring MVC 学习笔记 对locale和theme的支持 Locale Spring MVC缺省使用AcceptHeaderLocaleResolver来根据request header中的 ...

最新文章

  1. Luogu P1082 同余方程(NOIP 2012) 题解报告
  2. 目标10亿部?苹果AR眼镜有望明年登场!传搭载Mac级处理器、4K显示屏
  3. 【转】C++读写.mat文件的方法
  4. python子类初始化父类_Python实现子类调用父类的初始化实例
  5. 坑 之 TypeError: Cannot create initializer for non-floating point type.
  6. 深入学习二叉树(三) 霍夫曼树
  7. oracle判断是否包含字符串的方法
  8. 均匀线阵常规波束形成 matlab程序,波束形成Matlab程序
  9. 微信公众号网页授权-java开发
  10. mysql 不等于 符号写法
  11. 电子通信类顶级会议及期刊2(自用更新版)
  12. SEO是做什么的,每天需要做什么
  13. WinDbg命令详解--执行
  14. 请写出一段 python 代码实现删除一个 list 里面的重复元素
  15. LeetCode:Database 21.统计各专业学生人数
  16. 坐标变换学习笔记—代码篇Matlab
  17. Vscode的相对路径读取问题及处理
  18. 计算机excel2010运算符,excel2010公式中 计算运算符 使用基础教程
  19. 民航票务管理系统(C语言实现)
  20. 我们该如何保持独立思考?

热门文章

  1. CF643F-Bears and Juice【组合数学】
  2. P3243-[HNOI2015]菜肴制作【拓扑排序,优先队列】
  3. hdu5709-Claris Loves Painting【线段树合并】
  4. P3193-[HNOI2008]GT考试【KMP,dp,矩阵乘法】
  5. nssl1448-小智过马路【模拟】
  6. P1726-上白泽慧音【tarjan,图论】
  7. 2021“MINIEYE杯”中国大学生算法设计超级联赛(4)Display Substring(后缀数组+二分)
  8. JavaFX图表(二)之饼图
  9. insert ... on duplicate key update产生death lock死锁原理
  10. java图形验证码生成工具类