现在的位置: 首页 > 综合 > 正文

ClassPathXmlApplicationContext_filesystem方式获取applicationcontext配置文件信息

2013年12月09日 ⁄ 综合 ⁄ 共 1047字 ⁄ 字号 评论关闭

1

package com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class SpringExam {

	public static void main(String[] args) {
		ApplicationContext applicationContext=
				new ClassPathXmlApplicationContext ("com/applicationContext.xml");
		String path = SpringExam.class.getCanonicalName();
		System.out.println(path);
	}
}

配置文件路径带包名,这个是相对于编译后的 classpath

第二种方式用filesystem...

	public static void main(String[] args) {
		ApplicationContext applicationContext=
//				new ClassPathXmlApplicationContext ("com/applicationContext.xml");
				new FileSystemXmlApplicationContext ("src/com/applicationContext.xml");
		String path = SpringExam.class.getCanonicalName();
		System.out.println(path);
	}

这个路径要加上src,也不知道为啥

第三个方式

		new FileSystemXmlApplicationContext ("file:D:\\workspace\\j2ee\\springdemo\\src\\com\\applicationContext.xml");

第四个方式

		new FileSystemXmlApplicationContext ("D:\\workspace\\j2ee\\springdemo\\src\\com\\applicationContext.xml");

2

property文件要在src根目录下才行

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/xxx
username=xxx
password=xxx

抱歉!评论已关闭.