2010-06-11 69 views
3

內服務於我的服務層如何調用服務層

public class MyServiceLayerImpl{ 
    public void method1(){ 
     MyServicelayer.method(); //is this correct? 
    } 

    public void method2(){ 
    } 

    @Autowired 
    MyServiceInterface MyServiceLayer; 
} 

如果我有需要調用服務層內的另一個服務的服務層內的方法。我不能使用this._method,因爲我使用AOP進行緩存。爲了緩存起作用,我必須使用@Autowired才能獲得該服務。所以,上面的風格好嗎?

我得到以下錯誤

產生的原因:org.springframework.beans.factory.BeanCreationException:錯誤名爲 'com.company.iss.services.MyServiceLayerImpl#85aedd' 創建豆:字段自動裝配失敗;嵌套異常是org.springframework.beans.factory.BeanCreationException:無法自動裝入字段:com.company.iss.services.MyServicelayer com.company.iss.services.MyServiceLayerImpl.MyServiceLayer;嵌套異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有定義[com.company.iss.services.MyServiceLayer]類型的唯一bean:[interface com.company.iss.services.MyServiceLayer]類型的不滿意的依賴關係:預期的至少有1個匹配的豆

+1

你能提供更多的代碼和上下文嗎?我無法理解你正在嘗試做什麼... – davetron5000 2010-06-13 17:52:58

+0

@ davetron5000我更新了我的問題 – cometta 2010-06-14 01:23:59

回答

0

除了有一個大寫的變量和沒有冒號 - 沒關係。

您當然需要將您的類定義爲一個bean。無論是使用@Service(或其他定型)註釋就可以了,或者使用<bean>applicationContext.xmlsee here在春季2引入了基於註解的配置)

另一件事:你的成員變量應該是小寫,大寫沒有。

+0

我得到錯誤,如果我以這種方式調用方法。請看我的更新 – cometta 2010-06-14 09:42:03

+0

看我的...,;) – Bozho 2010-06-15 00:18:46

6

很難從怪異的格式和命名說,但如果你想從另一個調用一個服務:現在

public interface MasterService { 
    void someMethod(); 
} 

public class MasterServiceImpl implements MasterService { 
    private OtherService otherService; 

    public void someMethod() { 
    this.otherService.someCallOnOtherService(); 
    } 

    @Autowired 
    public void setOtherService(OtherService otherService) { 
    this.otherService = otherService; 
    } 
} 

,你必須配置了MasterServiceImpl和任何實現OtherService。有很多方法可以做到這一點,最流行的方法是在XML配置中顯式配置基於註釋的配置。

另請注意,如果您未使用接口,則AOP往往非常脆弱。在你的代碼中,你的Impl實際上並不是implement。我會建議反對。