spring xml配置注入改为手动注入过程

项目中需要使用MQ组件来接受消息,但是有的时候,在使用的时候,并不能满足spring注入的条件,无法注入。例如 在jfinal的config的afterJFinalStart中,由于jfinal集成spring,spring的注入是在controller调用之前拦截注入的,而在config中,并没有调用拦截器,所以没有注入。

那怎么办呢,只能手动注入

 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     
spring注入配置
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注入的时候,可以手动注入解决。

 

posted @ 2017-05-04 11:32  向_日_葵  阅读(1563)  评论(0编辑  收藏  举报