I've started playing with spring data elasticsearch and have been looking at the example here.
I am having trouble understanding how @Autowired
works. Consider the following:
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();
}
}
Whenever I run the unit test, articleService
in MyMessageProcessor
is always null
. Do I need extra configuration for the autowiring to work? All other code is the same as what is in the github repo linked above.
How do I ensure that wherever in my project I need to use ArticleServiceImpl
, it is autowired correctly?
I have seem other posts with the same issue but none of the solutions seem to work for my example below.