我已经开始使用 spring data elasticsearch 并且一直在查看这里的示例。
我无法理解如何@Autowired工作。考虑以下:
IMessageProcessor.java:
package message.processor;
public interface IMessageProcessor {
void processMessage();
}
MyMessageProcessor.java
package message.processor;
@Component
public class MyMessageProcessor implements IMessageProcessor {
@Autowired
private ArticleServiceImpl articleService;
private final Author johnSmith = new Author("John Smith");
private final Author johnDoe = new Author("John Doe");
@Override
public void processMessage() {
Article article = new Article("Spring Data Elasticsearch");
article.setAuthors(asList(johnSmith, johnDoe));
article.setTags("elasticsearch", "spring data");
articleService.save(article);
}
}
MyMessageProcessorIT.java
package message.processor;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class)
public class MyMessageProcessorIT {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Before
public void before() {
elasticsearchTemplate.deleteIndex(Article.class);
elasticsearchTemplate.createIndex(Article.class);
}
@Test
void testProcessMessage() {
MyMessageProcessor msgProcessor = new MyMessageProcessor();
msgProcessor.processMessage();
}
}
每当我运行单元测试时,articleServiceinMyMessageProcessor总是null. 我需要额外的配置才能使自动装配工作吗?所有其他代码与上面链接的 github 存储库中的相同。
如何确保在我需要使用的项目中的任何地方ArticleServiceImpl都正确地自动连接?
我似乎有过同样问题的其他帖子,但似乎没有一个解决方案适用于我下面的示例。
阿晨1998
婷婷同学_
慕斯王
相关分类