您的位置:首页 > 移动开发

【spring 区别】ClassXmlAplicationContext和FileSystemXmlApplicationContext的区别

2016-07-07 15:52 501 查看
今天写一个简单的spring使用例子,遇到这个问题:

项目结构如下:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import aoplog.LogAfterAdvice;
import aoplog.LogBeforeAdvice;
/**
* @author Michael
*
*/
public class TestApplicationContext {
/**
* @param args
*/
public static void main(String[] args) {
/**
* ClassPathXmlApplicationContext
*/
// 没有前缀:默认为项目的classpath下相对路径
ApplicationContext appCt = new ClassPathXmlApplicationContext(
"app.spring.xml");
// 前缀classpath:表示的是项目的classpath下相对路径
// ApplicationContext appCt = new ClassPathXmlApplicationContext(
// "classpath:app.spring.xml");
// 使用前缀file 表示的是文件的绝对路径
// ApplicationContext appCt = new ClassPathXmlApplicationContext(
// "file:D:/app.spring.xml");
LogBeforeAdvice logBefore = (LogBeforeAdvice) appCt
.getBean("logBefore");
System.out.println("ClassPathXmlApplicationContext test:"
+ logBefore.getClass());
// 利用通配符文件加载
ApplicationContext appCtXx = new ClassPathXmlApplicationContext(
"*.spring.xml");
// 多文件加载
String[] xmlCfg = new String[] { "classpath:base.spring.xml",
"myapp.spring.xml" };
ApplicationContext appCtMore = new ClassPathXmlApplicationContext(
xmlCfg);
/*
* FileSystemXmlApplicationContext
*/
// 默认为项目工作路径 即项目的根目录
ApplicationContext appCt2 = new FileSystemXmlApplicationContext(
"src/main/resources/app.spring.xml");
// 前缀classpath:表示的是项目的classpath下相对路径
// ApplicationContext appCt2 = new FileSystemXmlApplicationContext(
// "classpath:app.spring.xml");
// 使用前缀file 表示的是文件的绝对路径
// ApplicationContext appCt2 = new FileSystemXmlApplicationContext(
// "file:D:/app.spring.xml");
LogAfterAdvice logAfter = (LogAfterAdvice) appCt2.getBean("logAfter");
System.out.println("FileSystemXmlApplicationContext test:"
+ logAfter.getClass());
}
}


View Code

附录:

谈一谈什么是ClassPath以及怎么使用它:

首先:

java运行时的类路径,比如导入的类,在运行时需要将jar包放到classpath路径上。
classpath 在classpath路径上寻找指定文件,如果有多个符合的文件,以第一个为准,也就说,只要找到一个,就不在继续搜索
classpath*:会搜索所有满足条件的文件,有多少加载多少

其次:

编写的java源文件经过编译后生成class文件,需要指定目录存放这些文件。web程序默认都在web-info/classes目录下存放我们的java编译后的文件。
但是web-info目录下还有各种jar包和配置文件呢,确切说web-info根目录才是程序运行时classpath。放在根目录下的源代码,在编译之后,会将此文件copy到web-info目录。

JAVA获取classpath路径:

ClassLoader 提供了两个方法用于从装载的类路径中取得资源:

public URL getResource (String name);
public InputStream getResourceAsStream (String name);

这里name是资源的类路径,它是相对与“/”根路径下的位置。getResource得到的是一个URL对象来定位资源,而getResourceAsStream取得该资源输入流的引用保证程序可以从正确的位置抽取数据。
但是真正使用的不是ClassLoader的这两个方法,而是Class的 getResource和getResourceAsStream方法,因为Class对象可以从你的类得到(如YourClass.class或 YourClass.getClass()),而ClassLoader则需要再调用一次YourClass.getClassLoader()方法,不过根据JDK文档的说法,Class对象的这两个方法其实是“委托”(delegate)给装载它的ClassLoader来做的,所以只需要使用 Class对象的这两个方法就可以了。

因此,直接调用 this.getClass().getResourceAsStream(String name) ;获取流,静态化方法中则使用ClassLoader.getSystemResourceAsStream (String name) ; 。

下面是一些得到classpath和当前类的绝对路径的一些方法。你可能需要使用其中的一些方法来得到你需要的资源的绝对路径。

1.this.getClass().getResource("")
得到的是当前类class文件的URI目录。不包括自己!
如:file:/D:/workspace/jbpmtest3/bin/com/test/

2.this.getClass().getResource("/")
得到的是当前的classpath的绝对URI路径 。
如:file:/D:/workspace/jbpmtest3/bin/

3.this.getClass() .getClassLoader().getResource("")
得到的也是当前ClassPath的绝对URI路径 。
如:file:/D:/workspace/jbpmtest3/bin/

4.ClassLoader.getSystemResource("")
得到的也是当前ClassPath的绝对URI路径 。
如:file:/D:/workspace/jbpmtest3/bin/

5.Thread.currentThread().getContextClassLoader ().getResource("")
得到的也是当前ClassPath的绝对URI路径 。
如:file:/D:/workspace/jbpmtest3/bin/

6.ServletActionContext.getServletContext().getRealPath(“/”)
Web应用程序 中,得到Web应用程序的根目录的绝对路径。这样,我们只需要提供相对于Web应用程序根目录的路径,就可以构建出定位资源的绝对路径。
如:file:/D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/WebProject

注意点:

1.尽量不要使用相对于System.getProperty("user.dir")当前用户目录的相对路径。这是一颗定时炸 弹,随时可能要你的命。

2.尽量使用URI形式的绝对路径资源。它可以很容易的转变为URI,URL,File对象。

3.尽量使用相对classpath的相对路径。不要使用绝对路径。使用上面ClassLoaderUtil类的public static URL getExtendResource(String relativePath)方法已经能够使用相对于classpath的相对路径定位所有位置的资源。

4.绝对不要使用硬编码的绝对路径。因为,我们完全可以使用ClassLoader类的getResource("")方法得到当前classpath的绝对路径。如果你一定要指定一个绝对路径,那么使用配置文件,也比硬编码要好得多!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: