ClassPathXmlApplicationContext FileSystemXmlApplicationContext 转

最佳答案**

* 

* 

* 即:   对于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);
	}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(DAO,spring,tomcat,Web,xml)