我有一个配置类:
@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
中的callback
是null
或我错过了什么?
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
等)将不起作用。