最佳答案**
*
*
* 即: 对于ClassPathXmlApplicationContext(), classpath: 前缀是不需要的, 默认就是指项目的classpath路径下面;
* 如果要使用绝对路径,需要加上 file: 前缀表示这是绝对路径;
*
* 对于FileSystemXmlApplicationContext(), 默认表示的是两种:
* 1,没有盘符的是 项目工作路径, 即项目的根目录;
* 2,有盘符表示的是 文件绝对路径。
* (NOTE: Plain paths will always be interpreted as relative to the current VM working directory, even if they start with a slash. (This is consistent with the semantics in a Servlet container.) Use an explicit "file:" prefix to enforce an absolute file path. )
* 如果要使用classpath路径, 需要前缀 classpath: .
*
*/
public class HelloClient {
protected static final Log log = LogFactory.getLog(HelloClient.class);
public static void main(String[] args) {
// Resource resource = new ClassPathResource("appcontext.xml");
// BeanFactory factory = new XmlBeanFactory(resource);
//用classpath路径也可以
// ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:appcontext.xml");
// ApplicationContext factory=new ClassPathXmlApplicationContext("appcontext.xml");
// ApplicationContext factory=new ClassPathXmlApplicationContext("file:G:/1Java实用项目资源/2Spring/1精通Spring全Jar代码/workspace/workspace/example6/src/appcontext.xml");
//用文件系统的路径
// ApplicationContext factory=new FileSystemXmlApplicationContext("src/appcontext.xml");
//使用了 classpath: 前缀,作为标志, 这样,FileSystemXmlApplicationContext 也能够读入classpath下的相对路径
// ApplicationContext factory=new FileSystemXmlApplicationContext("classpath:appcontext.xml");
// ApplicationContext factory=new FileSystemXmlApplicationContext("file:G:/1Java实用项目资源/2Spring/1精通Spring全Jar代码/workspace/workspace/example6/src/appcontext.xml");
ApplicationContext factory=new FileSystemXmlApplicationContext("G:/1Java实用项目资源/2Spring/1精通Spring全Jar代码/workspace/workspace/example6/src/appcontext.xml");
IHelloWorld hw = (IHelloWorld) factory.getBean("helloworldbean");
log.info(hw.getContent("luoshifei"));
}
package com.baobaotao.service;
import java.util.Date;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.baobaotao.domain.User;
public class TestUserService extends TestCase {
public ApplicationContext factory = null;
private static String[] CONFIG_FILES = {
"baobaotao-dao.xml",
"baobaotao-service.xml" };
private static String[] CONFIG_FILES2 = {
"/WebRoot/WEB-INF/classes/baobaotao-dao.xml",
"/WebRoot/WEB-INF/classes/baobaotao-service.xml" };
protected void setUp() throws Exception {
factory = new FileSystemXmlApplicationContext(CONFIG_FILES2);
factory = new ClassPathXmlApplicationContext(CONFIG_FILES);
}
public void testHasMatchUser() {
UserService userService = (UserService) factory.getBean("userService");
boolean b1 = userService.hasMatchUser("admin", "123456");
boolean b2 = userService.hasMatchUser("admin", "1111");
assertTrue(b1);
assertTrue(!b2);
}
public void testFindUserByUserName() {
UserService userService = (UserService) factory.getBean("userService");
User user = userService.findUserByUserName("admin");
assertEquals(user.getUserName(), "admin");
}
public void testAddLoginLog() {
UserService userService = (UserService) factory.getBean("userService");
User user = userService.findUserByUserName("admin");
user.setUserId(1);
user.setUserName("admin");
user.setLastIp("192.168.12.7");
user.setLastVisit(new Date());
userService.loginSuccess(user);
}
}
private static ApplicationContext ctx = null;
static{
ctx = new FileSystemXmlApplicationContext("//usr/local/appliaction/SDLGateWayTest/WEB-INF/conf/applicationContent.xml");
}
如果只写一个/,比如:
private static ApplicationContext ctx = null;
static{
ctx = new FileSystemXmlApplicationContext("/usr/local/appliaction/SDLGateWayTest/WEB-INF/conf/applicationContent.xml");
}
这样,会默认在tomcat主目录下当做相对路径去寻找,就是去找/usr/local/tomcat6/usr/local/appliaction/SDLGateWayTest/WEB-INF/conf/applicationContent.xml 找不到,报错了。
=======================================
FileSystemXmlApplicationContext source code:
protected Resource getResourceByPath(String path) {
if (path != null && path.startsWith("/")) {
path = path.substring(1);
}
return new FileSystemResource(path);
}
分享到:
相关推荐
spring容器的触发事件 ClassPathXmlApplicationContext的start()方法的用法
spring读取配置不同目录下的配置文件,使用ClassPathXmlApplicationContext与FileSystemXmlApplicationContext
NULL 博文链接:https://gaojiewyh.iteye.com/blog/409606
NULL 博文链接:https://cuiran.iteye.com/blog/487385
二、ClassPathXmlApplicationContext[只能读放在web-info/classes目录下的配置文件]和FileSystemXmlApplicationContext的区别 classpath:前缀是不需要的,默认就是指项目的classpath路径下面; 如果要使用绝对路径,...
加载xml文件使用ClassPathXmlApplicationContext("applicationContext.xml") (3)BeanFactory(bean工厂)与ApplicationContext(容器)对比: 所有在xml里配置的Bean标签最后都会转换为BeanDefinition对象存放在...
NULL 博文链接:https://canfly2010.iteye.com/blog/443063
* ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。(更常用) * FileSystemXmlApplicationContext:它可以加载磁盘任意路径下的配置文件(必须有...
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); (2)ApplicationContext context = new FileSystemXmlApplicationContext ("applicationContext.xml"); 一般用第...
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); ``` 2. FileSystemXmlApplicationContext:从文件系统中加载配置文件,例如: ```java ApplicationContext ctx = new ...
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c1f10e: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c1f10e]; startup date [Wed ...
创建 ApplicationContext 接口实例有三种方法:通过 ClassPathXmlApplicationContext 创建、通过 FileSystemXmlApplicationContext 创建、通过 Web 服务器实例化 ApplicationContext 容器。 控制反转和依赖注入 ...
本例子用到Spring MVC , Jdbc, JSon, Gson 等等技术,Spring Servlet 配置,Spring中ClassPathXmlApplicationContext类的简单使用,Jdbc的配置与数据库的读取,List与json的转换,java对象使⽤Gson转json字符串null...
手写SpringIoc的XML实现方式,仿照Spring底层Bean工厂类实现ClassPathXmlApplicationContext
ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext-jms-producer.xml"); //获取生产者发送消息服务接口 MessageSender messageSender = (MessageSender)ac.getBean...
Jan 17, 2015 1:36:28 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41906a77:...
{ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); JdbcTemplate jdTemplate =(JdbcTemplate) applicationContext.getBean("jdbcTemplate"); jdTemplate....
在Java项目中,会通过ClassPathXmlApplicationContext类来实例化ApplicationContext容器。而在Web项目中,ApplicationContext容器的实例化工作会交由Web服务器来完成。 Web服务器实例化ApplicationContext容器时,...
public class ClassPathXmlApplicationContext implements BeanFactory { private Map, Object> beans = new HashMap, Object>(); @SuppressWarnings("unchecked") public ClassPathXmlApplicationContext() ...