Spring中ClassPathXmlApplicationContext和ApplicationContext 的简单使用
2017-09-06 17:37
267 查看
一、简单的用ApplicationContext做测试的话,获得Spring中定义的Bean实例(对象).可以用:
如果是两个
通配符:
二、ClassPathXmlApplicationContext[只能读放在web-info/classes目录下的配置文件]和FileSystemXmlApplicationContext的区别
classpath:前缀是不需要的,默认就是指项目的classpath路径下面;
如果要使用绝对路径,需要加上file:前缀表示这是绝对路径;
对于FileSystemXmlApplicationContext:
默认表示的是两种:
1.没有盘符的是项目工作路径,即项目的根目录;
2.有盘符表示的是文件绝对路径.
如果要使用classpath路径,需要前缀classpath:
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); RegisterDAO registerDAO = (RegisterDAO)ac.getBean("RegisterDAO");
如果是两个
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","dao.xml"});
通配符:
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*.xml");
二、ClassPathXmlApplicationContext[只能读放在web-info/classes目录下的配置文件]和FileSystemXmlApplicationContext的区别
classpath:前缀是不需要的,默认就是指项目的classpath路径下面;
如果要使用绝对路径,需要加上file:前缀表示这是绝对路径;
对于FileSystemXmlApplicationContext:
默认表示的是两种:
1.没有盘符的是项目工作路径,即项目的根目录;
2.有盘符表示的是文件绝对路径.
如果要使用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"); // ClassPathXmlApplicationContext使用了file前缀是可以使用绝对路径的 // ApplicationContext factory = new ClassPathXmlApplicationContext("file:F:/workspace/example/src/appcontext.xml"); // 用文件系统的路径,默认指项目的根路径 // ApplicationContext factory = new FileSystemXmlApplicationContext("src/appcontext.xml"); // ApplicationContext factory = new FileSystemXmlApplicationContext("webRoot/WEB-INF/appcontext.xml"); // 使用了classpath:前缀,这样,FileSystemXmlApplicationContext也能够读取classpath下的相对路径 // ApplicationContext factory = new FileSystemXmlApplicationContext("classpath:appcontext.xml"); // ApplicationContext factory = new FileSystemXmlApplicationContext("file:F:/workspace/example/src/appcontext.xml"); // 不加file前缀 ApplicationContext factory = new FileSystemXmlApplicationContext("F:/workspace/example/src/appcontext.xml"); IHelloWorld hw = (IHelloWorld)factory.getBean("helloworldbean"); log.info(hw.getContent("luoshifei")); } }
相关文章推荐
- spring中ClassPathXmlApplication和FileSystemXmlApplicationContext使用区别
- 使用Spring中的ClassPathXmlApplicationContext加载配置文件
- ClassPathXmlApplicationContext的简单使用
- 浅析Spring中ClassPathXmlApplicationContext()的装配过程
- 【Spring学习笔记-2】Myeclipse下第一个Spring程序-通过ClassPathXmlApplicationContext加载配置文件
- spring mvc使用ClassPathXmlApplicationContext或FileSystemXmlApplicationContext和XmlWebApplicationContext类的操作其中 XmlWebApplicationContext是专为Web工程定制的。
- Spring ClassPathXmlApplicationContext getBean创建实例
- Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext
- Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext
- junit4 的使用 顺便理解ClassPathXmlApplicationContext的使用
- Spring中关于ClassPathXmlApplicationContext和FileSystemXmlApplicationContext的路径设置
- spring加载bean(ClassPathXmlApplicationContext和FileSystemXmlApplicationContext)
- 使用MyEclipse整合Struts+Spring+Hibernate时,在“applicationContext.xml”中提示class"org.apache.commons.dbcp.Basi
- spring学习(五)ClassPathXmlApplicationContext的getBean获取实例bean
- Spring的ClassPathXmlApplicationContext在Eclipse中的路径问题
- 使用MyEclipse整合Struts+Spring+Hibernate时,在“applicationContext.xml”中提示class"org.apache.commons.dbcp.Basi
- Spring 的ClassPathXmlApplicationContext()路径读取错误
- Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext
- Spring中BeanFacory的模拟实现(ClassPathXMLApplicationContext)
- Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext