Home > Software design >  How to debug autowired interface in configuration class
How to debug autowired interface in configuration class

Time:12-16

I have a configuration class:

@Configuration
public class XDDeliveryConfig {

    @Value("${direct.gateway.xd.endpointUrl: http://localhost:8080/xd/services/DocumentRepository_Service}")
    protected String endpointURL;

    @Autowired
    protected RoutingResolver resolver;

    @Autowired
    protected XDDeliveryCallback xdDeliveryCallback;

    @Bean
    @ConditionalOnMissingBean
    public XDDeliveryCore xdDeliveryCore() {
        final ReliableDispatchedNotificationProducer notificationProducer =
            new ReliableDispatchedNotificationProducer(
                new NotificationSettings(true, "Direct XD Delivery Agent",
                        "Your message was successfully dispatched."));

        return new XDDeliveryCore(
                resolver, xdDeliveryCallback, new DefaultTxDetailParser(),
                new DefaultMimeXdsTransformer(), new DocumentRepository(), 
                notificationProducer, endpointURL);
    }
}

Which for some reason has a problem with the autowired XDDeliveryCallback interface which looks like:

public interface XDDeliveryCallback {
    void sendNotificationMessage(NotificationMessage message) throws MessagingException;
}

And constructor for the XDDeliveryCore class looks like:

public class XDDeliveryCore {
    protected final RoutingResolver resolver;
    protected final XDDeliveryCallback callback;
    protected final TxDetailParser txParser;
    protected final MimeXdsTransformer mimeXDSTransformer;
    protected final DocumentRepository documentRepository;
    protected final NotificationProducer notificationProducer;
    protected final String endpointUrl;

    public XDDeliveryCore(RoutingResolver resolver, XDDeliveryCallback callback,
                          TxDetailParser txParser,
                          MimeXdsTransformer mimeXDSTransformer, DocumentRepository documentRepository,
                          NotificationProducer notificationProducer, String endpointUrl) {
        this.resolver = resolver;
        this.callback = callback;
        this.txParser = txParser;
        this.mimeXDSTransformer = mimeXDSTransformer;
        this.endpointUrl = endpointUrl;
        this.documentRepository = documentRepository;
        this.notificationProducer = notificationProducer;
    }

}

How can I debug why callback in the XDDeliveryCore is null or what I have missed?

PS. Implementation for the interface:

@Configuration
public class XDRemoteDeliveryProcessor implements XDDeliveryCallback {

    @Autowired
    protected XDDeliveryCore deliveryCore;

    @Autowired
    protected SmtpGatewayMessageSource smtpMessageSource;

    @Autowired
    protected DSNCreator dsnCreator;

    @Bean
    public Consumer<Message<?>> directXDDeliveryInput() {
        return streamMsg -> {
            try {
                final SMTPMailMessage smtpMessage = SMTPMailMessageConverter.fromStreamMessage(streamMsg);

                log.debug("XDRemoteDeliveryProcessor processing message from "   smtpMessage.getMailFrom().toString());
                deliveryCore.processAndDeliverXDMessage(smtpMessage);
            } catch (MessagingException e) {
                throw new RuntimeException(e);
            }
        };
    }

    public void sendNotificationMessage(NotificationMessage message) throws MessagingException {
        smtpMessageSource.sendMimeMessage(message);
    }
}

CodePudding user response:

You want to autowire a bean, an instance of the XDDeliveryCallback interface. Though the XDRemoteDeliveryProcessor is the implementation of the interface, it is not registered as a Spring bean (I assume there is no XML configuration as your code seems fully annotation based).

The solution is to either register a bean in the XML configuration using <bean class=... > element or rather annotate the XDRemoteDeliveryProcessor implementation with @Component/@Service.

As long as the bean is properly scanned (using @SpringBootApplication/@Configuration or <context:component-scan base-package=... >), the bean is registered and the autowiring successful.

@Component
public class XDRemoteDeliveryProcessor implements XDDeliveryCallback {

    // implementation
}

Remember the Spring annotations (such as @Autowired, @Bean, etc.) take no effect if the class is not registered as a proper Spring bean on configuration.

  • Related