有没有办法使用@autowired标签注入(inject)子对象之一?
public class Provider {
@Autowired
private ParentFeedAPI feedAPI;
}
public abstract class ParentFeedAPI {
}
public class Child1FeedAPI extends ParentFeedAPI{
}
public class Child2FeedAPI extends ParentFeedAPI{
}
最佳答案
您应该遵守@Qualifier注释,并使用例如命名你的bean @Component (或者更合适的 spring 构造型注释),类似于
public class Provider {
@Qualifier("child1")
@Autowired
private ParentFeedAPI feedAPI;
}
public interface ParentFeedAPI {
}
@Component("child1")
public class Child1FeedAPI implements ParentFeedAPI{
}
@Component("child2")
public class Child2FeedAPI implements ParentFeedAPI{
}
此外,尝试针对接口(interface)进行编程
关于java - 如何在 Spring 中使用 @autowired 标签在子对象之间切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36223288/