1.使用类加载器
类加载器可以解析任何类型的文件,文件只能在java项目src目录下
class对象.getClassLoader().getResourceAsStream("文件名");
获取class对象的方式:
a. 对象.getClass(),
b. 类名.class()
c. Class.forName("类路径")
2.使用ResourceBundle ,只能解析properties文件
ResourceBundle rb = ResourceBundle.getBundle("文件名")
3,ClassPathXmlApplicationContext,解析xml文件,在项目中,使用dom4j方式解析
ApplicationContext context = new ClassPathXmlApplicationContext("文件名")
User user = (User)context.getBean("bean id") bean id 为user
4. FileSystemXmlApplicationContext,解析xml文件,在磁盘下