?
? ????? PHP ??? ???? ??? ?? ??
ResourceLoaderAware
是特殊的標(biāo)記接口,它希望擁有一個(gè)ResourceLoader
引用的對象。
public interface ResourceLoaderAware { void setResourceLoader(ResourceLoader resourceLoader); }
當(dāng)實(shí)現(xiàn)了 ResourceLoaderAware
接口的類部署到application context(比如受Spring管理的bean)中時(shí),它會(huì)被application context識別為 ResourceLoaderAware
。
接著application context會(huì)調(diào)用setResourceLoader(ResourceLoader)
方法,并把自身作為參數(shù)傳入該方法(記住,所有Spring里的application context都實(shí)現(xiàn)了ResourceLoader
接口)。
既然 ApplicationContext
就是ResourceLoader
,那么該bean就可以實(shí)現(xiàn) ApplicationContextAware
接口并直接使用所提供的application context來載入資源,但是通常更適合使用特定的滿足所有需要的
ResourceLoader
實(shí)現(xiàn)。
這樣一來,代碼只需要依賴于可以看作輔助接口的資源載入接口,而不用依賴于整個(gè)Spring ApplicationContext
接口。
從Spring 2.5開始, 你可以使用ResourceLoader
的自動(dòng)裝配來代替實(shí)現(xiàn) ResourceLoaderAware
接口?!皞鹘y(tǒng)的” constructor
及 byType
的自動(dòng)裝配模式 (第?3.3.5?節(jié) “自動(dòng)裝配(autowire)協(xié)作者”已有論述)現(xiàn)在可以分別為構(gòu)造方法參數(shù)及setter方法參數(shù)提供 ResourceLoader
類型的依賴。請使用新式的基于注解的自動(dòng)裝配特性以提供更大的靈活性(包括裝配屬性及多個(gè)方法參數(shù)的能力)。在這種情況下,只要屬性、構(gòu)造方法或者方法被 @Autowired
注解修飾,ResourceLoader
就會(huì)被裝配到需要ResourceLoader
類型的屬性、構(gòu)造方法參數(shù)或者方法參數(shù)中。請查看章節(jié) 第?3.11.1?節(jié) “@Autowired
”以了解進(jìn)一步的信息。