参考文章:http://my.oschina.net/xiaoxishan/blog/381209#OSC_h3_7
一,步骤参照参考文献
二、新建的项目
三、补充
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 5 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 6 7 <display-name>activemq</display-name> 8 9 <!-- Spring ApplicationContext配置文件的路径,可使用通配符,用于后面的Spring Context Loader --> 10 <context-param> 11 <param-name>contextConfigLocation</param-name> 12 <param-value> 13 classpath:applicationContext.xml 14 </param-value> 15 </context-param> 16 17 <!--Spring ApplicationContext 载入 --> 18 <listener> 19 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 20 </listener> 21 22 <!-- Spring MVC Servlet --> 23 <servlet> 24 <servlet-name>dispatcher</servlet-name> 25 <servlet-class>com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet</servlet-class> 26 <init-param> 27 <param-name>contextConfigLocation</param-name> 28 <param-value>classpath:DispatcherServlet.xml</param-value> 29 </init-param> 30 <load-on-startup>1</load-on-startup> 31 </servlet> 32 <servlet-mapping> 33 <servlet-name>dispatcher</servlet-name> 34 <url-pattern>/</url-pattern> 35 </servlet-mapping> 36 37 38 <welcome-file-list> 39 <welcome-file>index.html</welcome-file> 40 </welcome-file-list> 41 </web-app>
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:goldfish="http://www.fangdd.com/schema/goldfish" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 9 http://www.fangdd.com/schema/goldfish http://www.fangdd.com/schema/goldfish/goldfish-1.0.0.xsd"> 10 11 <context:annotation-config/> 12 13 <context:component-scan base-package="com.zp.test" > 14 </context:component-scan> 15 16 17 <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 --> 18 <context:component-scan base-package="com.activemqtest.*"/> 19 20 <!-- 配置JMS连接工厂 --> 21 <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> 22 <property name="brokerURL" value="failover:(tcp://localhost:61616)" /> 23 </bean> 24 25 <!-- 定义消息队列(Queue) --> 26 <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> 27 <!-- 设置消息队列的名字 --> 28 <constructor-arg> 29 <value>queue1</value> 30 </constructor-arg> 31 </bean> 32 33 <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 --> 34 <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> 35 <property name="connectionFactory" ref="connectionFactory" /> 36 <property name="defaultDestination" ref="queueDestination" /> 37 <property name="receiveTimeout" value="10000" /> 38 </bean> 39 40 <!--queue消息生产者 --> 41 <bean id="producerService" class="com.activemqtest.serviceImpl.ProducerServiceImpl"> 42 <property name="jmsTemplate" ref="jmsTemplate"></property> 43 </bean> 44 45 <!--queue消息消费者 --> 46 <bean id="consumerService" class="com.activemqtest.serviceImpl.ConsumerServiceImpl"> 47 <property name="jmsTemplate" ref="jmsTemplate"></property> 48 </bean> 49 50 <!-- 定义消息队列(Queue),我们监听一个新的队列,queue2 --> 51 <bean id="queueDestination2" class="org.apache.activemq.command.ActiveMQQueue"> 52 <!-- 设置消息队列的名字 --> 53 <constructor-arg> 54 <value>queue2</value> 55 </constructor-arg> 56 </bean> 57 58 <!-- 配置消息队列监听者(Queue),代码下面给出,只有一个onMessage方法 --> 59 <bean id="queueMessageListener" class="com.activemqtest.serviceImpl.QueueMessageListener" /> 60 61 <!-- 消息监听容器(Queue),配置连接工厂,监听的队列是queue2,监听器是上面定义的监听器 --> 62 <bean id="jmsContainer" 63 class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 64 <property name="connectionFactory" ref="connectionFactory" /> 65 <property name="destination" ref="queueDestination2" /> 66 <property name="messageListener" ref="queueMessageListener" /> 67 </bean> 68 <!-- 定义消息主题(Topic) --> 69 <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> 70 <constructor-arg> 71 <value>JY_topic</value> 72 </constructor-arg> 73 </bean> 74 <!-- 配置JMS模板(Topic),pubSubDomain="true"--> 75 <bean id="topicJmsTemplate" class="org.springframework.jms.core.JmsTemplate"> 76 <property name="connectionFactory" ref="connectionFactory" /> 77 <property name="defaultDestination" ref="topicDestination" /> 78 <property name="pubSubDomain" value="true" /> 79 <property name="receiveTimeout" value="10000" /> 80 </bean> 81 <!--topic消息发布者 --> 82 <bean id="topicProvider" class="com.activemqtest.serviceImpl.TopicProvider"> 83 <property name="topicJmsTemplate" ref="topicJmsTemplate"></property> 84 </bean> 85 <!-- 消息主题监听者 和 主题监听容器 可以配置多个,即多个订阅者 --> 86 <!-- 消息主题监听者(Topic) --> 87 <bean id="topicMessageListener" class="com.activemqtest.serviceImpl.TopicMessageListener" /> 88 <!-- 主题监听容器 (Topic) --> 89 <bean id="topicJmsContainer" 90 class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 91 <property name="connectionFactory" ref="connectionFactory" /> 92 <property name="destination" ref="topicDestination" /> 93 <property name="messageListener" ref="topicMessageListener" /> 94 </bean> 95 96 </beans>
DispactcherServlet.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 9 10 <context:annotation-config /> 11 <context:component-scan base-package="com.zp.test.controller"> 12 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 13 </context:component-scan> 14 </beans>
四、运行结果
因为运行了两遍,所以是双数
五、遇到的问题
问题:Java.lang.IllegalStateException: Failed to load ApplicationContext
原因:因为applicationContext里的包没有与实际的包对应,或者是没有注解导入对应的变量