2016-09-19 49 views
0

我使用@Configuration,並設置屬性ehCacheConfig@Value註釋:佔位性質不解決

@Configuration 
public class CacheConfig {  
    @Value("${configPath}") 
    private Resource ehCacheConfig; 
} 

但在運行項目錯誤時,如下發生。由於錯誤表明佔位符屬性configPath未被spring容器解析。誰能告訴我爲什麼?

java.io.FileNotFoundException:無法打開ServletContext的資源 [/ $ {}用configPath] org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141) org.springframework .cache.ehcache.EhCacheManagerUtils.parseConfiguration(EhCacheManagerUtils.java:107)

回答

0

您需要定義一個PropertySource從那裏加載configPath的價值:

@Configuration 
@PropertySource("classpath:/com/myco/app.properties") 
public class CacheConfig {  

    @Value("${configPath}") 
    private Resource ehCacheConfig; 

} 

app.properties

configPath: /home/[...]/myConfig.xml 
+0

我添加的屬性在文件<上下文:屬性佔位符忽略資源未找到= 「真」 位置= 「類路徑*:base.properties」/> ,我想這可以使它像使用PropertySource – lawrence

+0

你可以發佈base.properties的內容? – codependent

+0

configPath = classpath:ehcache.xml authenticationCacheName = authenticationCache authorizationCacheName = authorizationCache – lawrence