项目中需要使用MQ组件来接受消息,但是有的时候,在使用的时候,并不能满足spring注入的条件,无法注入。例如 在jfinal的config的afterJFinalStart中,由于jfinal集成spring,spring的注入是在controller调用之前拦截注入的,而在config中,并没有调用拦截器,所以没有注入。
那怎么办呢,只能手动注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616?wireFormat.maxInactivityDuration=0</value>
</property>
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref bean="connectionFactory"/>
</property>
</bean>
<bean id="messagesender" class="com.supermap.sm3dad.messagequeue.CMessageSender">
<property name="jmsTemplate" ref="jmsTemplate"/>
<!-- <property name="destination" ref="destination"/> -->
</bean>
<bean id="messagereceiver" class="com.supermap.sm3dad.messagequeue.CMessageReceiver">
<property name="jmsTemplate" ref="jmsTemplate"/>
<!-- <property name="destination" ref="destination"/> -->
</bean> <!-- <bean id="config" class="com.demo.config.Config">
<property name="messagereceiver" ref="messagereceiver"/>
<property name="destination" ref="destination"/>
</bean> --> </beans>
spring注入配置
public ActiveMQHelper(String p_brokerURL, long p_reciveTimeout) { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL(p_brokerURL);
jmsTemplate.setConnectionFactory(connectionFactory);
jmsTemplate.setReceiveTimeout(p_reciveTimeout);
}
手动注入
在不能使用spring注入的时候,可以手动注入解决。