[英]Spring : @Autowired not working
這不是重復的問題。 請回答這個問題,我正在使用Spring 4.0.2。 這是基於Web的應用程序,我也使用RabbitMQ Server一次處理多個作業。 我在控制器服務和Daos中使用了@Autowired對象,它工作正常。
但是,當我們在RabbitMQ偵聽器中使用bean時。 它給出Null。 看一看。
這是項目樹
這是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類。
此類實際上是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>
當我運行該項目時,將創建並實例化Bean。 控制台結果。
我嘗試了什么解決方案:
1)從命令提示符處清理並構建項目-mvn -X clean install
2)檢查組件掃描-<context:component-scan base-package =“ in.xxxx.broker” />
3)使用applicationContext.getBean(“ idOfBean”)
4)將變量名稱-> TRANSFORMER_COMPONENT_URL更改為transUrl
5)更改工作區
6)嘗試在新安裝的Eclipse上
但是,它仍然為空。
更新:1
在Servlet-context.xml文件中,我嘗試通過從屬性文件中獲取值來創建Bean並初始化TRANSFORMER_COMPONENT_URL的值,以及從TransFormerClient類中刪除了@component和@Value注釋。
<bean id="transformerClient" class="in.cdac.broker.client.TransformerClient">
<property name="TRANSFORMER_COMPONENT_URL" value="${TRANSFORMER_COMPONENT_URL}"/>
</bean>
自動布線在這里工作正常。 我確實知道原因。
您需要集成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配置:
<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/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.