2014-10-20 149 views
0

我現在有一個應用程序context.cml:豆不自動裝配

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd" 
    default-autowire="byType"> 


    <context:annotation-config /> 
    <tx:annotation-driven /> 

    <bean class="app.bl.facade.impl.DishFacadeImpl" /> 

而且我試圖自動裝配它:

@Autowired 
private DishFacade dishFacade; 

public List<Dish> getEvents() { 
    System.out.println(dishFacade); 
    return dishFacade.getAll(); 
} 

結果我被打印出來是null,我但無法弄清楚爲什麼。

+3

是您的DishFacade也由Spring創建的類嗎? – SMA 2014-10-20 12:52:36

+1

我相信你需要添加'' – forgivenson 2014-10-20 12:52:38

+0

其中'' – 2014-10-20 12:52:55

回答

0

假設你的包是app.bl.facade你必須把這個標籤在你application-context.xml

<context:component-scan base-package="app.bl.facade" /> 

上面的XML標籤將執行自動掃描。應該創建爲bean的每個類都使用@Component(用於一般bean)或@Controller(用於servlet控制器)或@Repository(用於DAO類)或@Service的正確stereotype註釋進行註釋,並且這些類應放置在相同的基礎下包app.bl.facade

如果滿足上述條件,那麼spring將自動找到所有這些條件併爲每條條件創建一個bean。

+0

爲什麼他需要這個?,因爲他正在使用註釋配置。他需要的是在DishFacade的setter上使用@Autowiring,或者使Dishter的SetterFacade爲 – Subh 2014-10-20 13:01:45

+0

Spring bean將自動掛接到其他彈簧託管bean,除非外部setter或構造函數屬性設置爲執行注入過程。 – 2014-10-20 13:03:55

0

您的默認自動佈線模式是「byType」。對於這種類型的自動裝配,使用類類型。所以在spring bean配置文件中應該只有一個爲這種類型配置的bean。

所以,你應該是自動裝配DishFacadeImpl而不是DishFacade其中,我認爲,是接口。

This可能會有所幫助。