@Autowired对象始终为null

时间:2019-03-06 12:05:47

标签: java spring spring-boot autowired spring-data-elasticsearch

我已经开始研究spring数据elasticsearch,并一直在研究示例here

我无法理解@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();
    }
}

每当我运行单元测试时,articleService中的MyMessageProcessor始终是null。我需要额外的配置才能使自动布线工作吗?所有其他代码与上面链接的github存储库中的代码相同。

如何确保在项目中需要使用ArticleServiceImpl的任何地方,它都能正确自动接线?

我似乎有其他帖子也有同样的问题,但是以下示例似乎都无法解决该问题。

3 个答案:

答案 0 :(得分:1)

您不是在使用Spring创建的MyMessageProcessor实例并在此行上创建自己的实例。

MyMessageProcessor msgProcessor = new MyMessageProcessor();

您应该在测试类中自动连接MyMessageProcessor。

答案 1 :(得分:0)

因为您不允许使用spring框架创建MyMessageProcessor,因为spring不会注入您的ArticleServiceImpl

@Configuration
public class Configuration { 

@Bean
public MyMessageProcessor mymessageProcessor() {
 return new MyMessageProcessor();
}
}

然后 测试类中的@Autowire MyMessageProcessor

答案 2 :(得分:0)

@PCL,您帖子中的第一个评论链接提供了完整的答案,但是在您的代码中,您有两种选择: 1-将var entries = [{ name: 'Bergman', manager: 'Alfredo', title: 'senior engineer' }, { name: 'Billy', manager: 'Alfredo', title: 'senior engineer' }, { name: 'Johnson', manager: 'Alfredo', title: 'department manager' }, { name: 'Angela', manager: 'Johnson', title: 'senior engineer' }, { name: 'Anderson', manager: 'Johnson', title: 'senior engineer' }, { name: 'Amy', manager: 'Johnson', title: 'department manager' }, { name: 'Patsy', manager: 'Amy', title: 'engineer' }, { name: 'Jane', manager: 'Amy', title: 'engineer' }, { name: 'Alfredo', manager: '', title: 'general manager' }] function build_org_chart(list) { var map = {}, node, managers = [], i; // Build the map of employees we will use later. for (i = 0; i < list.length; i += 1) { map[list[i].name] = i; list[i].directReports = []; // init the children } for (i = 0; i < list.length; i += 1) { node = list[i]; if (node.manager != "") { // If the employee has a manager, find them var indexOfManager = map[node.manager]; // Then add that employee to the managers children. list[indexOfManager].directReports.push(node); } else { // If they haven't got a manager, must be a boss. managers.push(node); } } // Return only the top bosses. return managers; } console.log(build_org_chart(entries));自动接线:

MyMessageProcessor msgProcessor

或者,您获得应用程序上下文并手动将Bean自动装配起来,在@Tom提供的链接中,您可以看到具有手动bean查找的内容。如果要创建一个新bean,则applicationContext还有一个自动装配方法。

最好的问候, 大卫