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