2017-03-01 57 views
1

我有一個原型bean工廠(CreateCarAction是彈簧豆,範圍=原型):自動裝配抽象組件

@Component("createCarActionFactory") 
public abstract class CreateCarActionFactory { 

    @Lookup 
    public abstract CreateCarAction createCarAction(); 
} 

和我自動裝配它像這樣:

@Autowired 
@Qualifier("createCarActionFactory") 
private CreateCarActionFactory createCarActionFactory; 

它,當我刪除抽象的工作原理從CreateCarActionFactory。當我刪除抽象我必須實現createCarAction() - 我不需要這個實現,因爲@Lookup覆蓋它。當我從CreateCarActionFactory辭職我必須訪問CreateCarAction形式的beanfactory,所以我JAVE自動裝配Bean工廠,這也是醜..

我可以自動裝配抽象CreateCarActionFactory@Component

+0

爲什麼不使用提供者? '''Provider createCarActionProvider;'''然後你只需要'''createCarActionProvider.get()'''。 – hya

+2

你不能autowire抽象類 - http://stackoverflow.com/a/15971422/1814524 – hya

+0

唉 - 我不明白,在哪裏將實施這樣的提供者?它將如何創建帶有自動佈線彈簧的新類?這就是爲什麼我使用Lookup的原因。 – michealAtmi

回答

0

您可以autowire接口,所以將CreateCarActionFactory從抽象類更改爲接口。

+0

NoSuchBeanDefinitionException:找不到符合依賴關係的[mypackage.CreateCarActionFactory]類型的合格bean:期望至少1個符合此依賴關係的autowire候選者。依賴註釋:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} – michealAtmi