gpt4 book ai didi

java - @Autowired 任意配置方法

转载 作者:行者123 更新时间:2023-12-02 03:45:30 26 4
gpt4 key购买 nike

在 Spring 引用指南中,它说您可以将 @Autowired 注释应用于具有任意名称和/或多个参数的方法,如以下代码所示。

public class MovieRecommender {

private MovieCatalog movieCatalog;

private CustomerPreferenceDao customerPreferenceDao;

@Autowired
public void prepare(MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}

// ...

}

但是Spring容器不会调用prepare()方法,因为它不是一个setter方法。 @Autowired 在这种情况下如何工作?

最佳答案

它并没有说你可以将 @Autowired 用于它所说的任何方法

Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container.

<beans>
<bean id="myBean" class="..." init-method="prepare"/>
</beans>

关于java - @Autowired 任意配置方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27993355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com