Java 为什么jmsTemplate总是空的?使用spring和ApacheActiveMQ

Java 为什么jmsTemplate总是空的?使用spring和ApacheActiveMQ,java,spring,jms,mule,activemq,Java,Spring,Jms,Mule,Activemq,我对Spring和JMS非常陌生。我一直在尝试提出一个涉及activemq和Spring的实现,如下所示 spring-context.xml <bean id="sampleApacheConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" lazy-init="true"> <property name="brokerURL" value="tcp://localhost:61

我对Spring和JMS非常陌生。我一直在尝试提出一个涉及activemq和Spring的实现,如下所示

spring-context.xml
<bean id="sampleApacheConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" lazy-init="true">
    <property name="brokerURL" value="tcp://localhost:61616"/>
    <property name="userName" value=“kodeseeker"/>
    <property name="password" value=“mypassword"/>

</bean>

 <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <constructor-arg ref="sampleApacheConnectionFactory" />
    </bean>

    <!--  Default Destination Queue Definition-->
    <bean id="defaultDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0" value="test.Foo"/>
    </bean>

    <!-- JmsTemplate Definition -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="defaultDestination" ref="defaultDestination" />
    </bean>

    <!-- Message Sender Definition -->
    <bean id="messageSender" class="com.mypackage.Publisher2">
    </bean>

<!-- Message Receiver Definition -->
    <bean id="messageReceiver" class="com.mypackage.Listener">

    </bean>
       <bean class="org.springframework.jms.listener.SimpleMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destinationName" value="test.Foo" />
        <property name="messageListener" ref="messageReceiver" />
    </bean>

</beans>
为了初始化
jmsTemplate
,我有什么特别需要做的吗?如有必要,我很乐意提供更多细节

编辑1: 类调用发布更新

public class UpdateHandlerImpl implements UpdateHandler {
    private final Publisher2 publisher;
    ....
    public UpdateHandlerImpl() {
        this(new Publisher2());
    }
    public UpdateHandlerImpl(
            final Publisher2 publisher) {
           this. publisher = publisher;
    }
    ....
    @Override
    public void  handle(final CustomMessage entity) {
        try {
                     publisher. publishUpdate(entity);
        } catch (final JMSException e) {
            LOGGER.error("Error sending message", e);
        }

            }
   …..
    }
编辑3: 基于@keith输入的UpdateHandlerImpl的更新版本

public class UpdateHandlerImpl implements UpdateHandler {
    //Hoping spring wires this?
    Publisher2 publisher;
    @Override
    public void  handle(final CustomMessage entity) {
        try {
                     publisher. publishUpdate(entity);
        } catch (final JMSException e) {
            LOGGER.error("Error sending message", e);
        }

            }
   …..
    }
编辑2: spring上下文在启动时使用以下注释通过mule(这是一个mule应用程序)加载

<spring:beans>
        <spring:import resource="classpath:spring-context.xml" />
    </spring:beans>

如果使用
new
创建
Publisher2
,则创建的实例上不会连接依赖项。相反,在您的上下文文件中将其定义为Springbean并从中获取它

编辑 正如我所怀疑的,在您对问题的最新更新中,您确认您正在创建一个带有新

public UpdateHandlerImpl() {
        this(new Publisher2());
    }

这不是Spring的工作方式。请参阅Spring框架文档中有关实例化bean的部分。(简而言之,使用上下文创建bean

显示调用
publishUpdate
@chrylis的类。增加了细节。我希望它有所有需要的信息,如果不自由问。我可以提供更多的上下文。谢谢你的回答。。但是我是否已经在基于您提供的链接的spring上下文文件
中这样做了呢?您不应该在代码中实例化任何希望spring为您连接的对象。在本例中,
newpublisher2())
正在阻止您使用依赖项注入,因为它不是由框架管理的bean<代码>并删除了实例化。使新班级变得像。但是jmsTemplate仍然是空的。我错过了什么?春天与“希望”无关。您必须告诉Spring在类中注入依赖项,要么使用字段上的自动连线注释和类上的组件(使其成为Springbean),要么使用XML中的bean定义(您还必须将UpdateHandlerImpl定义为Springbean)。希望。。。。太有趣了,但听了很失望。
public UpdateHandlerImpl() {
        this(new Publisher2());
    }