spring-code详解

来源:互联网 发布:淘宝好的男装店 编辑:程序博客网 时间:2024/06/04 20:07

spring code 为spring容器的核心。 有beans,beanFactory,beandefinitions,applicationContext等定义。实现了控制反转与依赖注入bean配置以及加载。

1.实例化beanFactory

1.1 加载classPath下的配置文件

ClassPathResource context = new ClassPathResource("applicationContext.xml");

XmlBeanFactory factory = new XmlBeanFactory(context);

Iservice service = factory.getBean("service");

1.2使用文件流加载任意位置的文件

InputStream in = new FileInputStream("c:\\ApplicationContext.xml");

XmlBeanFactory factory = new XmlBeanFactory(in);

1.3使用classPathXMLApplicationContext加载多个配置文件

ClassPathXMLApplicationContext context  = new ClassPathXMLApplicationContext(new String[]{"ApplicationContext.xml","ApplicationContext-part2.xml"})
BeanFactory Factory = (BeanFactory)context;
原创粉丝点击