項目中須要使用MQ組件來接受消息,可是有的時候,在使用的時候,並不能知足spring注入的條件,沒法注入。例如 在jfinal的config的afterJFinalStart中,因爲jfinal集成spring,spring的注入是在controller調用以前攔截注入的,而在config中,並無調用攔截器,因此沒有注入。spring
那怎麼辦呢,只能手動注入apache
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:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 10 11 <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> 12 <property name="brokerURL"> 13 <value>tcp://localhost:61616?wireFormat.maxInactivityDuration=0</value> 14 </property> 15 </bean> 16 <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> 17 <property name="connectionFactory"> 18 <ref bean="connectionFactory"/> 19 </property> 20 </bean> 21 <bean id="messagesender" class="com.supermap.sm3dad.messagequeue.CMessageSender"> 22 <property name="jmsTemplate" ref="jmsTemplate"/> 23 <!-- <property name="destination" ref="destination"/> --> 24 </bean> 25 <bean id="messagereceiver" class="com.supermap.sm3dad.messagequeue.CMessageReceiver"> 26 <property name="jmsTemplate" ref="jmsTemplate"/> 27 <!-- <property name="destination" ref="destination"/> --> 28 </bean> 29 30 <!-- <bean id="config" class="com.demo.config.Config"> 31 <property name="messagereceiver" ref="messagereceiver"/> 32 <property name="destination" ref="destination"/> 33 </bean> --> 34 35 </beans> 36
1 public ActiveMQHelper(String p_brokerURL, long p_reciveTimeout) { 2 3 ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(); 4 connectionFactory.setBrokerURL(p_brokerURL); 5 jmsTemplate.setConnectionFactory(connectionFactory); 6 jmsTemplate.setReceiveTimeout(p_reciveTimeout); 7 }
在不能使用spring注入的時候,能夠手動注入解決。tcp