在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
工作正常。但是在尝试使用属性文件时,它会在启动服务器时抛出错误
有人可以帮我解决这个问题吗?
答案 0 :(得分:3)
<强>更新强> 使用
<util:properties id="mcbPage" location="classpath:mcb.properties"/>
然后在你的Bean
private @Value("#{mcbPage['name']}") String strDefaultPage;
答案 1 :(得分: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