3

在 Spring MVC Hibernate 应用程序中,当我尝试使用位于 src/java/resources 下的属性文件时,它会抛出以下错误:

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'

我正在使用下面的代码来访问我的控制器类中的属性值:

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

我在我的 ApplicationContext.xml 文件中为这个属性文件添加了 bean:

<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>

我的属性文件 ( mcb.properties) 位于 src/main/resources 下。@Autowired工作正常。但是当尝试使用属性文件时,它会在启动服务器时引发错误。
有人可以帮我解决这个问题吗?

4

2 回答 2

3

更新 使用

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

然后在你的 Bean

private @Value("#{mcbPage['name']}") String strDefaultPage;
于 2012-06-30T18:15:54.867 回答
0

您还可以使用 @PropertySource("classpath: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
于 2012-07-01T14:26:05.183 回答