实例化Spring Ioc文件的三种方式(硬编码)

1.使用文件系统载入Resource进行加载实例化

Resource resource = new FileSystemResource("C:\\Users\\Zhuyj\\workspace\\SpringLearning\\src\\beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
System.out.println(factory);

2.从Class Path中载入Resource进行加载实例化

Resource resource = new ClassPathResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
System.out.println(factory);
3.从context中进行加载实例化

ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
BeanFactory factory = (BeanFactory)context;
System.out.println(factory);

注:在web工程中,通常使用在web.xml文件中配置监听器的方式实现Spring实例化



你可能感兴趣的:(java,spring)