为什么我的服务总是在实现类中返回NULL?

时间:2017-04-26 11:44:16

标签: spring rabbitmq

有人可以帮助我,我被迫RabbitmqSpring一起实施,但我的服务无效!

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();
        }
    }

}

2 个答案:

答案 0 :(得分:0)

您必须使用一些Spring组件(例如@Service)注释CsvService类。

@Service
public class CsvService  {
 //Code Here
}

并且

public class QueWorker implements MessageListener {

    @Autowired 
    private CsvService csvService;
}

如果您已经这样做但仍然不起作用,那么在类路径扫描中弹出这一点是不可见的。在Spring applicatgion main中,您需要添加

@ComponentScan(basePackage="")

添加您的包名。

答案 1 :(得分:0)

我找到了通过创建AutowireHelper.java和AutowireHelperConfig.java解决这个问题的工作

将它放在spring config文件夹&gt; util的

  

AutowireHelper.java

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;
    }

}
  

AutowireHelperConfig.java

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();
    }

}
  

在实现类中调用服务而不获取NULL 自动装配您的服务然后使用 AutowireHelper.autowire(this,this.csvService)调用它,请参阅下面的示例!

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'");
        }
    }
}