Spring 为什么我的服务在实现类中总是返回NULL?
有人能帮我吗?我一直在尝试用Spring实现Rabbitmq,但我的服务不起作用Spring 为什么我的服务在实现类中总是返回NULL?,spring,rabbitmq,Spring,Rabbitmq,有人能帮我吗?我一直在尝试用Spring实现Rabbitmq,但我的服务不起作用 public class QueWorker implements MessageListener { @Inject/@Autowired not working T-T private CsvService csvService; @Override public void onMessage(Message message) { // System.out.
public class QueWorker implements MessageListener {
@Inject/@Autowired not working T-T
private CsvService csvService;
@Override
public void onMessage(Message message) {
// System.out.println(new String(message.getBody()));
Gson gsonGet = new Gson();
TypeToken<Map> json = new TypeToken<Map>() {};
Map msgGet = gsonGet.fromJson(new String(message.getBody()), json.getType());
Map data = (Map) msgGet.get("data");
ArrayList containerData = (ArrayList) msgGet.get("containerData");
try {
csvService.excutePermit(containerData, data);
} catch (Exception e){
e.printStackTrace();
}
}
}
您必须用一些Spring组件示例@Service注释CsvService类
@Service
public class CsvService {
//Code Here
}
及
如果您已经这样做了,但仍然不起作用,那么spring在类路径扫描中一定看不到这一点。在Spring Application main中,您需要添加
@ComponentScan(basePackage="")
添加您的包名。我发现可以通过创建AutowireHelper.java和AutowireHelperConfig.java来解决此问题 将其放在spring配置文件夹>util下 AutowireHelper.java AutowireHelperConfig.java 在实现类中调用服务而不获取NULL Autowire您的服务,然后使用AutowireHelper.autowirethis,this.csvService调用它请参见下面的示例
你是怎么联系QueWorker的?Autowiring仅在spring托管bean中有效,如果您使用新关键字创建类则无效Yes我在QueConfigit中启动了QueWorker try put@ComponentScanbasePackage=com.company.app仍然没有给我nullShare您的springConfig.xml、CsvService和QueWorker java类。你一定配置了不正确的东西。我想我找到了解决问题的方法。谢谢:D
@ComponentScan(basePackage="")
package com.yourcompany.yourapplicationname.config.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* Helper class which is able to autowire a specified class.
* It holds a static reference to the {@link org.springframework.context.ApplicationContext}.
*/
public final class AutowireHelper implements ApplicationContextAware {
private static final AutowireHelper INSTANCE = new AutowireHelper();
private static ApplicationContext applicationContext;
AutowireHelper() {
}
/**
* Tries to autowire the specified instance of the class if one of the specified beans which need to be autowired
* are null.
*
* @param classToAutowire the instance of the class which holds @Autowire annotations
* @param beansToAutowireInClass the beans which have the @Autowire annotation in the specified {#classToAutowire}
*/
public static void autowire(Object classToAutowire, Object... beansToAutowireInClass) {
for (Object bean : beansToAutowireInClass) {
if (bean == null) {
applicationContext.getAutowireCapableBeanFactory().autowireBean(classToAutowire);
}
}
}
@Override
public void setApplicationContext(final ApplicationContext applicationContext) {
AutowireHelper.applicationContext = applicationContext;
}
/**
* @return the singleton instance.
*/
public static AutowireHelper getInstance() {
return INSTANCE;
}
}
package com.yourcompany.yourapplicationname.config.util;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* configuration class to create an AutowireHelper bean
*/
@Configuration
public class AutowireHelperConfig {
@Bean
public AutowireHelper autowireHelper(){
return new AutowireHelper();
}
}
public class QueWorker implements MessageListener {
@Autowired
private CsvService csvService;
@Override
public void onMessage(Message message) {
Map msgGet = (Map) SerializationUtils.deserialize(message.getBody());
Map<String, Object> data = (Map) msgGet.get("data");
ArrayList containerData = new ArrayList((Collection) msgGet.get("containerData"));
try {
AutowireHelper.autowire(this, this.csvService);
csvService.excutePermit(containerData, data);
System.out.println(" [x] Received Message'");
} catch (Exception e){
e.printStackTrace();
} finally {
System.out.println(" [x] Done Process Message'");
}
}
}