【问题标题】:Dependency injection with context.getbeans使用 context.getbeans 进行依赖注入
【发布时间】:2017-03-04 07:28:57
【问题描述】:

这是我获取 bean 的控制器代码

@RequestMapping("/suscribetest")
    @ResponseBody
    public String subscribeTest(){
        try{
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MqttInboundBeans.class);
            MqttPahoMessageDrivenChannelAdapter messageChannel = context.getBean("inbound",MqttPahoMessageDrivenChannelAdapter.class);
            messageChannel.addTopic("test", 2);


        }catch(Exception ex){
            System.out.println("err "+ex.getLocalizedMessage());
        }
        return "";
    }

下面是我的 bean 类

@Configuration
public class MqttInboundBeans {
  @Autowired
private UserService service;


@Bean
public MessageChannel mqttInputChannel() {
    return new DirectChannel();
}

@Bean
public MessageProducer inbound() {
    MqttPahoMessageDrivenChannelAdapter adapter =
            new MqttPahoMessageDrivenChannelAdapter("tcp://localhost:1883", "testClient",
                                             "DATA/#", "LD/#");
    adapter.setCompletionTimeout(0);
    adapter.setConverter(new DefaultPahoMessageConverter());
    adapter.setQos(2);
    adapter.setOutputChannel(mqttInputChannel());
    return adapter;
}

@Bean
@ServiceActivator(inputChannel = "mqttInputChannel")
public MessageHandler handler() {
    return new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            System.out.println(message.getHeaders()+"  "+message.getPayload());

        }

    };
}
}

当我尝试运行应用程序时,它工作正常,我可以从 messageHandler 获取消息,但是当我尝试在运行时从控制器 getBean 时出现错误

Error creating bean with name 'mqttInboundBeans': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ehydromet.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

谢谢。

【问题讨论】:

    标签: spring dependency-injection javabeans


    【解决方案1】:

    bean UserService 不是由 MqttInboundBeans 生成的。 AnnotationConfigApplicationContext 创建 Spring Application Context 接受输入作为我们使用 @Configuration 注解的配置类,注册所有由配置类在 Spring 运行时生成的 bean。 尝试自动连接应用程序上下文,然后从中获取 bean。

    @Autowired
    private ApplicationContext context;
    @RequestMapping("/suscribetest") @ResponseBody public String subscribeTest(){ 
    try{ 
     MqttPahoMessageDrivenChannelAdapter messageChannel =(MqttPahoMessageDrivenChannelAdapter) context.getBean("inbound");
    
    
    messageChannel.addTopic("test", 2); ....
    

    【讨论】:

    • 我在 MqttInboundBeans.java 中有一个自动连接的服务,所以如果我要使用新关键字创建上下文,那么我如何设法为 UserService 注入依赖项
    • AnnotationConfigApplicationContext 只会给你在 MqttInboundBeans 中定义的 bean。 ApplicationContext 将包含项目中定义的所有 bean。
    猜你喜欢
    • 2015-09-26
    • 2014-01-19
    • 2014-03-25
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多