中国个人优秀网站,一般网站设计多少钱,wordpress主题个性,网站分析 案例spring boot注释1.简介 在以前的文章中#xff0c;我们学习了如何使用Spring JMS配置项目。 如果查看有关使用Spring JMS进行消息传递的文章介绍 #xff0c;您会注意到它是使用XML配置的。 本文将利用Spring 4.1版本中引入的改进 #xff0c;并仅使用Java config配置JMS项目… spring boot注释 1.简介 在以前的文章中我们学习了如何使用Spring JMS配置项目。 如果查看有关使用Spring JMS进行消息传递的文章介绍 您会注意到它是使用XML配置的。 本文将利用Spring 4.1版本中引入的改进 并仅使用Java config配置JMS项目。 在这个示例中我们还将看到使用Spring Boot配置项目是多么容易。 在开始之前请注意您可以像往常一样查看下面示例中使用的项目的源代码。 请参阅github上的示例项目 。 栏目 介绍。 示例应用程序。 设置项目。 一个使用JMS侦听器的简单示例。 使用SendTo将响应发送到另一个队列。 结论。 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。 parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion1.2.3.RELEASE/versionrelativePath/ !-- lookup parent from repository --
/parent 这个父级基本上设置了几个Maven默认值并为我们将要使用的主要依赖项提供了依赖项管理例如Spring版本4.1.6。 重要的是要注意此父pom定义了许多库的版本但未对我们的项目添加任何依赖关系。 因此不必担心会得到不使用的库。 下一步是设置Spring Boot的基本依赖关系 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId
/dependency 除了核心Spring库之外此依赖项还将带来Spring Boot的自动配置功能。 这将允许框架尝试根据您添加的依赖项自动设置配置。 最后我们将添加Spring JMS依赖项和ActiveMQ消息代理将整个pom.xml保留如下 groupIdxpadro.spring/groupId
artifactIdjms-boot-javaconfig/artifactId
version0.0.1-SNAPSHOT/version
packagingjar/packaging
nameJMS Spring Boot Javaconfig/nameparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion1.2.3.RELEASE/versionrelativePath/ !-- lookup parent from repository --
/parentpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingstart-classxpadro.spring.jms.JmsJavaconfigApplication/start-classjava.version1.8/java.versionamq.version5.4.2/amq.version
/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-jms/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.apache.activemq/groupIdartifactIdactivemq-core/artifactIdversion${amq.version}/version/dependency
/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-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.persistentfalse,useShutdownHookfalse;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 ListOrder receivedOrders new ArrayList();Overridepublic void registerOrder(Order order) {this.receivedOrders.add(order);}Overridepublic OptionalOrder 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));OptionalOrder 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.htmlspring boot注释