春天:@Autowired不工作

时间:2015-10-29 10:45:53

标签: java spring-mvc rabbitmq autowired spring-rabbit

这不是一个重复的问题。请仔细阅读这个问题我正在使用Spring 4.0.2。它是基于Web的应用程序,我也使用RabbitMQ Server一次处理多个作业。我在控制器服务和Daos中使用了@Autowired对象它工作正常。

但是当我们在RabbitMQ Listeners中使用bean时。它给了Null。看看。

这是Project Tree

Project Tree

这是TransformerClient类快照。

@Component
public class TransformerClient {

     @Value("${TRANSFORMER_COMPONENT_URL}")
     private String TRANSFORMER_COMPONENT_URL;

     public TransformerClient() {
          super();
          logger.info("Instantiated " + getClass());
     }

     public static final Logger logger = LoggerFactory
        .getLogger(TransformerClient.class);
....
}

这是TCListener Class。

这个类实际上是RabbitMq监听器。当任何作业被推入队列时,它分别负责执行此过程。自动onMessage()函数将执行。

public class TCListener implements MessageListener {

    private static final Logger logger = LoggerFactory
        .getLogger(TCListener.class);

    @Autowired
    TransformerClient transformerClient;

    public TCListener() {
           super();
          logger.info("Instantiated " + getClass());
    }

    @override
    public void onMessage(Message message){
         if(transformerClient !=null)
               logger.info("Not null");
         else
               logger.info("null");
   }
}

Servlet配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


    <!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure -->
    <!-- <context:property-placeholder location="classpath:amqp.properties"/> -->
    <context:property-placeholder
    system-properties-mode="ENVIRONMENT" location="classpath:*.properties" />

    <!-- Enables the Spring MVC @Controller programming model -->
    <!-- enable anno support -->
    <context:annotation-config />
    <!-- anno based req mapping controller -->
    <mvc:annotation-driven />

    <context:component-scan base-package="in.xxxx.broker" />
   .....
</beans:beans>

当我运行这个项目时,Beans正在创建和实例化。控制台结果。

Console

  

我尝试了哪些解决方案:

     
    

1)从命令提示符清理和构建项目 - mvn -X clean install

         

2)检查组件扫描 - &lt; context:component-scan base-package =“in.xxxx.broker”/&gt;

         

3)使用applicationContext.getBean(“idOfBean”)

         

4)更改变量名称 - &gt; TRANSFORMER_COMPONENT_URL到transUrl

         

5)更改了工作区

         

6)尝试新安装的Eclipse

  

但是它仍然是空的。

  

更新:1

在Servlet-context.xml文件中,我尝试通过从属性文件中获取值以及从TransFormerClient类中删除@component和@Value注释来创建Bean并初始化TRANSFORMER_COMPONENT_URL的值。

<bean id="transformerClient" class="in.cdac.broker.client.TransformerClient">
        <property name="TRANSFORMER_COMPONENT_URL" value="${TRANSFORMER_COMPONENT_URL}"/>  
</bean>

此处自动装配工作正常。我确实得到了理由。

1 个答案:

答案 0 :(得分:0)

您需要整合RabbitMQ和spring。你可以在这里找到,怎么做:

你可以这样写:

import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@EnableRabbit //need for activation of handler for annotation @RabbitListener
@Component
public class TCListener {
     private static final Logger logger = LoggerFactory
        .getLogger(TCListener.class);

    @Autowired
    TransformerClient transformerClient;

    @RabbitListener(containerFactory="myRabbitListenerContainerFactory", queues="myQueue")
    public void onMessage(String message) {
        if(transformerClient !=null){
               logger.info("Not null");
        }else{
               logger.info("null");
        }
    }
}

还需要添加到spring config:

 <rabbit:annotation-driven container-factory="myRabbitListenerContainerFactory"/>

     <bean id="myRabbitListenerContainerFactory"  class="org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory">
           // factory settings
     </bean>

您还可以查看下一个指南:https://spring.io/guides/gs/messaging-rabbitmq/