我正在使用Spring 4.2.1
。我搜索了stackoverflow,以及许多在线示例......但似乎没有任何工作。我相信该程序正在查找属性文件,因为我没有收到IOException
的{{1}}。
有什么想法吗?我无法理解我做错了什么。这是我第一次接触Spring。
这是我的FileNotFoundException
文件
Beans.xml
这是我的<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="propertyConfigurator"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<description>
Configurer that replaces ${...} placeholders with values
from a properties file
</description>
<property name="locations">
<list>
<value>classpath*:user.properties</value>
</list>
</property>
</bean>
<bean id="user" class="com.my.User">
<description>
Load Properties of the user bean from user.properties
file
</description>
<property name="email" value="${user.email}" />
</bean>
</beans>
文件
user.properties
创建用户并打印用户电子邮件的主要应用
user.email=user@yoyo.com
一个简单的package com.my;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
User usr = (User) context.getBean("user");
System.out.println("Email = " + usr.getEmail());
}
}
类:
User
最后,堆栈跟踪:
package com.my;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 0L;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public User() {
}
}
答案 0 :(得分:1)
我们遇到了类似的问题,我所做的研究建议使用:
<context:property-placeholder location="classpath*:user.properties" local-override="true" ignore-resource-not-found="true"/>
代替你的
<bean id="propertyConfigurator"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<description>
Configurer that replaces ${...} placeholders with values
from a properties file
</description>
<property name="locations">
<list>
<value>classpath*:user.properties</value>
</list>
</property>
</bean>