2012-06-30 41 views
3

在Spring MVC Hibernate應用程序,當我嘗試使用屬性文件,它是在src/JAVA /資源,它拋出以下錯誤:在Spring MVC Hibernate應用程序中使用屬性文件時拋出錯誤?

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.mcb.controller.UserController.strDefaultPage; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'mcbPage.name' 

我使用下面的代碼中訪問屬性值我控制器類:

<bean id="mcbProperties" 
    class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
    <property name="ignoreResourceNotFound" value="true" /> 
    <property name="locations"> 
     <list> 
      <value>classpath*:mcb.properties</value> 
      <value>file:src/main/resources/mcb.properties</value> 
     </list> 
    </property> 
</bean> 

<bean id="propertyConfigurer" 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> 
    <property name="ignoreUnresolvablePlaceholders" value="true" /> 
    <property name="properties" ref="mcbProperties" /> 
</bean> 

和我的財產音響:

@Value("${mcbPage.name}") 
private String strDefaultPage; 

我在這個屬性文件我applicationContext.xml文件添加豆le(mcb.properties)位於src/main/resources下。 @Autowired工作正常。但是當嘗試使用propert文件時,它在啓動服務器時出現錯誤。
有人可以幫我解決這個問題嗎?

+0

你如何註冊屬性文件? –

+0

@JigarJoshi,現在我已經添加上面的ApplicaitonContext.xml文件。請看一下。 – kamal

+0

你是如何建立你的項目?如果你在eclipse中運行,你是否啓用了autobuild?另外,是否將src/main/resources設置爲構建路徑中的源文件夾?它很可能不會將它放到你的classes目錄中。 – Matt

回答

3

更新 使用

<util:properties id="mcbPage" location="classpath:mcb.properties"/> 

,然後在你的豆

private @Value("#{mcbPage['name']}") String strDefaultPage; 
+0

** @ Jigar **,不工作。給出相同的錯誤... – kamal

+0

你有屬性文件中的'name = someValue'條目? –

+0

是的,我有名稱=值。 – kamal

0

您還可以使用@PropertySource( 「類路徑:mysql.properties」)

或者

1) Add this to your spring config XML . 

    <bean id="messageSource" class="org.springframework.context.support. 
       ResourceBundleMessageSource"> 
     <property name="basename" value="messages"/> 
    </bean> 

2) create a property file name calles "messages.properties" and place that in 
WEB-INF/Classes folder. 

3) Include the following JSTL in jsp. 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 

4)Use the properties as follows in JSP. 

<fmt:message key="yourPropertyName"/> 

5) Make sure you place "messages" in Web-inf/classes 
相關問題