java - 在 Spring MVC Hibernate 应用程序中使用属性文件时抛出错误?

标签 java hibernate spring-mvc

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

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

@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 工作正常。但是当尝试使用属性文件时,它会在启动服务器时抛出错误。
有人可以帮我解决这个问题吗?

最佳答案

更新 使用

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

然后在你的 Bean 中

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

关于java - 在 Spring MVC Hibernate 应用程序中使用属性文件时抛出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11276363/

相关文章:

java - 如何在 Ant 中将 java 项目转换为插件项目?

java - 没有代码生成的 Jooq insert 语句

java - 使用java servlet将excel文件发送到客户端

hibernate - Grails读取GORM映射

spring - 在 hibernate/spring 中的多个模式之间分区数据

java - <表格 :select> objects binding through Converter in Spring MVC

java - 未能延迟初始化角色多对多关系的集合

java - spring mvc 3.0之后没有改变重定向url

spring - MockMvc 返回 null 而不是对象

java - Hibernate获取同一个对象到不同的对象