ClassXmlAplicationContext、FileSystemXmlApplicationContext

[color=red]blog迁移至[/color]:[url=http://www.micmiu.com]http://www.micmiu.com[/url]

本人在测试一个方法时需要加载XML配置文件,spring提供了相应的方法,就小小研究了下,在此记录下具体的过程,方便初学者和自己日后回顾。

Spring容器最基本的接口就是BeanFactory. BeanFactory负责配置、创建、管理Bean,它有一个子接口ApplicationContext,也称为Spring上下文。Spring容器负责管理Bean与Bean之间的信赖关系。

BeanFactory有很多实现类,通常使用org.springframework.beans.factory.xml.XmlBeanFactory类。但对于大部分J2EE应用而言,推荐使用ApplicationContext. ApplicationContext是BeanFactory的子接口,其常用实现类是[color=red]org.springframework.context.support.FileSystemXmlApplicationContext[/color]和[color=red]org.springframework.context.support.ClassXmlAplicationContext[/color]。
Springr的配置信息通常采用XML配置文件来设置,因此,创建BeanFactory实例时,应该提供XML配置文件作为参数。、

[size=medium]下面详细介绍ApplicationContext的实际运用:[/size]

[color=red]一:ClassPathXmlApplicationContext[/color]
[color=blue]1.没有前缀:默认为项目的classpath下相对路径[/color]
ApplicationContext appCt = new ClassPathXmlApplicationContext("app.spring.xml");

[color=blue]2.前缀classpath:表示的是项目的classpath下相对路径[/color]
ApplicationContext appCt = new ClassPathXmlApplicationContext("classpath:app.spring.xml");

[color=blue]3.使用前缀file 表示的是文件的绝对路径[/color]
ApplicationContext appCt = new ClassPathXmlApplicationContext("file:D:/app.spring.xml");

[color=blue]4.可以同时加载多个文件[/color]
String[] xmlCfg = new String[] { "classpath:base.spring.xml","app.spring.xml"};
ApplicationContext appCt = new ClassPathXmlApplicationContext(xmlCfg);

[color=blue]5.使用通配符加载所有符合要求的文件[/color]
ApplicationContext appCt = new ClassPathXmlApplicationContext("*.spring.xml");

[color=red]二:FileSystemXmlApplicationContext[/color]
[color=blue]1.默认为项目工作路径 即项目的根目录[/color]
ApplicationContext appCt2 = new FileSystemXmlApplicationContext("src/main/resources/app.spring.xml");

[color=blue]2.前缀classpath:表示的是项目的classpath下相对路径[/color]
ApplicationContext appCt2 = new FileSystemXmlApplicationContext("classpath:app.spring.xml");

[color=blue]3.使用前缀file 表示的是文件的绝对路径[/color]
ApplicationContext appCt2 = new FileSystemXmlApplicationContext("[color=red]file:D:/app.spring.xml[/color]");
ApplicationContext appCt2 = new FileSystemXmlApplicationContext("[color=red]D:/app.spring.xml[/color]");

[color=blue]4.可以同时加载多个文件[/color]
String[] xmlCfg = new String[] { "src/main/resources/base.spring.xml","classpath:app.spring.xml"};
ApplicationContext appCt2 = new FileSystemXmlApplicationContext(xmlCfg);

[color=blue]5.使用通配符加载所有符合要求的文件[/color]
ApplicationContext appCt2 = new FileSystemXmlApplicationContext("classpath:*.spring.xml");

[color=red]详细代码如下:[/color]
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());
}

}

就先写到这的,有什么疑问或者错误欢迎大家指正。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值