2015-05-29 33 views
0

我有這樣的定義,在我的應用程序上下文:春找不到性質

<beans xmlns:context="http://www.springframework.org/schema/context" 
    ... 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 

<context:property-placeholder location="classpath:propfile.properties"/> 

我propfile.properties:

parallelism=4 

而且因爲它是一個Maven項目的一部分,我所在這個文件像這樣的:現在

-src/main/java 
-src/main/resources 
--propfile.properties 

,在我的組件:

@WebService 
@Component 
public class MyService{ 
     @Value("#{parallelism}") 
     private Integer parallelism; 

     .... 

} 

在初始化階段,它給了我

字段或屬性「並行」不能 類型的對象中找到「org.springframework.beans.factory.config.BeanExpressionContext

爲什麼?此外,在Web應用程序環境中使用屬性佔位符是否正確?或者是否有其他方法來設置屬性?

+0

您的屬性佔位符與您的組件掃描是否在同一個彈簧配置文件中?它在不同的彈簧配置文件中,你是否在導入它? – ConMan

+5

另外#前綴引用春季表達式語言查詢,如果您引用屬性文件,您應該使用$前綴。 – ConMan

回答

0

嘗試如下更改配置文件:

<bean id="propfile" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
    <property name="locations"> 
    <list> 
     <value>classpath*:propfile.properties</value> 
    </list> 
    </property> 
</bean> 

和組件爲:

@WebService 
@Component 
public class MyService{ 
     @Value("${parallelism}") 
     private Integer parallelism; 

     .... 

} 

此外,您還可以使用如下,獲得完整的屬性文件:

 @Component 
     public class MyService{ 
       @Resource(name="propfile") 
       private Properties propfile; 

       .... 

    }