簡體   English   中英

為什么運行時依賴項類在編譯時可用?

[英]Why are classes of runtime dependency available at compile time?

我正在eclipse中的maven-spring項目上工作,我在下面聲明了依賴項:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.0.0.RELEASE</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

據我了解,org.springframework.context中的類僅在運行時可用,而在編譯時不可用。 但是我可以在代碼中從上述包中導入類,而沒有任何編譯錯誤(在Eclipse中)。 這是我的代碼:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloWorld {

public static void main(String args[])
{
    ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
}

}

在這里,我的基本問題是上述代碼是如何工作的。 當我嘗試使用ApplicationContext時,應該出現編譯時錯誤,因為我在運行時范圍中包含了“ spring-context”構件? 我的月食在這里做些招嗎? 我在這里想念什么? 謝謝

在運行時可能需要編譯時間,也可能不需要。 如果是編譯范圍內的依賴項,則jar將被復制到您的類路徑中,因此將在運行時使用。 另一方面,您可以使用某些依賴項編譯代碼,並且在運行時可以在運行范圍內使用該依賴項的其他實現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM