如何调试配置类中的自动连接接口



我有一个配置类:

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

由于某种原因,它与自动连接的XDDeliveryCallback接口有问题,看起来像:

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

XDDeliveryCore类的构造函数如下:

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

我如何调试为什么XDDeliveryCore中的callbacknull或我错过了什么?

p。接口的实现:

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

您想要自动装配一个bean,一个XDDeliveryCallback接口的实例。虽然XDRemoteDeliveryProcessor是接口的实现,但它没有注册为Spring bean(我假设没有XML配置,因为您的代码似乎完全基于注释)。

解决方案是使用<bean class=... >元素在XML配置中注册bean,或者使用@Component/@Service注释XDRemoteDeliveryProcessor实现。

只要bean被正确扫描(使用@SpringBootApplication/@Configuration<context:component-scan base-package=... >), bean就被注册并且自动装配成功。

@Component
public class XDRemoteDeliveryProcessor implements XDDeliveryCallback {
// implementation
}

请记住,如果在配置时没有将类注册为正确的Spring bean,那么Spring注释(如@Autowired@Bean等)将不起作用。

最新更新