首先是个User类:
package spring_introduction;
public class User {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void hello(){
System.out.println("hello "+name);
}
}
配置文件Name.properties
name=Lucy
下面是HelloName 输出类:
package spring_introduction;
import java.io.IOException;
import java.util.Properties;
import sun.org.mozilla.javascript.internal.ast.Name;
public class HelloName {
public static void main(String[] args) {
User user = new User();
String name="";
Properties prop = new Properties();
try {
prop.load(HelloName.class.getClassLoader().getResourceAsStream("Name.properties"));
//此处HelloName若不在静态方法中调用,可以用this代替
name=prop.getProperty("name");
} catch (IOException e) {
e.printStackTrace();
}
user.setName(name);
user.hello();
}
}