找不到匹配的bean类型。使用发布者和消费者时

时间:2017-05-10 07:11:38

标签: java spring queue activemq

public class ListenerAndPublisher implements ErrorHandler{


    private static final Logger LOG = LoggerFactory
            .getLogger(ListenerAndPublisher.class);


    // URL of the JMS server. DEFAULT_BROKER_URL will just mean
    // that JMS server is on localhost
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    // default broker URL is : tcp://localhost:61616"

    private static String subject = "trip.be.queue"; //Queue Name
    // You can create any/many queue names as per your requirement.
    //Queue name-VALLYSOFTQ

    public static void mainWEST(Message message) throws JMSException
    {
        TextMessage textMessage = (TextMessage) message;
        // Getting JMS connection from the server and starting it
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();
        connection.start();
        // JMS messages are sent and received using a Session. We will
        // create here a non-transactional session object. If you want
        // to use transactions you should set the first parameter to 'true'
        Session session = connection.createSession(false,
        Session.AUTO_ACKNOWLEDGE);
        // Destination represents here our queue 'VALLYSOFTQ' on the
        // JMS server. You don't have to do anything special on the
        // server to create it, it will be created automatically.
        Destination destination = session.createQueue(subject);
        // MessageProducer is used for sending messages (as opposed
        // to MessageConsumer which is used for receiving them)
        MessageProducer producer = session.createProducer(destination);
        // We will send a small text message saying 'Hello' in Japanese
        TextMessage messageXML = session.createTextMessage(textMessage.getText());
        // Here we are sending the message!
        producer.send(message);
        System.out.println("Sentage '" + messageXML.getText() + "'");

        connection.close();
}
    @Override
    public void handleError(Throwable arg0) {
        // TODO Auto-generated method stub

    }

}

上面是我的Publisher类,下面是我的另一个监听器类

@Named("someRandom")
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class ListenerParent extends
        SessionAwareMessageListener<Message> {

@Inject
    private ListenerAndPublisher listenerAndPublisher;
@Override
    public void onMessage(Message message, Session session)
            throws JMSException, RuntimeException {

        LOG.debug("----------------- Queue Listener--------------");

        try {
listenerAndPublisher.mainWEST(message);
}
catch (FMSRuntimeException | RuntimeException e) {
            LOG.error("Exception processing message: {}", e.toString());
            // throw new RuntimeException("Rolling Back to queue!!!");
        } catch (NamingException e) {
            LOG.error("Exception processing mesage: {}", e.toString());
        } catch (FMSException e) {
        }
}

这里我尝试从ListenerParent调用ListenerAndPublisher中的方法,如上面的代码所示。但是在部署我的maven spring项目时我遇到了异常。有帮助吗?非常感谢!!!!!

Cannot resolve reference to bean 'someRandom' while setting bean property 'messageListener'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'someRandom': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.west.proxy.vcg.b.beans.ListenerAndPublisher com.west.proxy.vcg.b.beans.ListenerParent.listenerAndPublisher; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.west.proxy.vcg.b.beans.ListenerAndPublisher] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}

1 个答案:

答案 0 :(得分:0)

在类中添加@Component可以解决此问题。