最简单的容器,提供了基础的依赖 注入支持。创建各种类型的Bean.
bean工厂介绍
工厂设计模式,创建分发各种bean。配置好它们之间的写作关系,参与bean的生命周期。
bean工厂只把bean的定义信息载进来,用到的时候才实例化。
方法1:
方法2:
方法3:
package www.csdn.spring.beanfactory.test;
import static org.junit.Assert.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import www.csdn.spring.service.HelloService;
import www.csdn.spring.service.HelloServiceImpl;
public class FactoryTest {
@Test
public void test() {
try {
//BeanFactory c; //new XmlBeanFactory(new InputStreamResource(new FileInputStream("E:\\workspaces\\sday02\\src\\spring.xml")));
//BeanFactory factory = new XmlBeanFactory((Resource) new FileInputStream("E:\\workspaces\\sday02\\src\\spring.xml"));
ClassPathResource resource = new ClassPathResource("spring.xml");
/*
* InputStreamResource不能够使用了。
InputStreamResource res = new InputStreamResource(new FileInputStream("E:\\workspaces\\sday02\\src\\spring.xml"));
* isOpen(){ return true;}
*/
FileSystemResource fileSystemResource = new FileSystemResource("E:\\workspaces\\sday02\\src\\spring.xml");
BeanFactory factory = new XmlBeanFactory(fileSystemResource);
//beanFactory = new ClassPathXmlApplicationContext("spring.xml");
//beanFactory = new FileSystemXmlApplicationContext("E:\\workspaces\\sday02\\src\\spring.xml");
HelloService helloService = factory.getBean("helloServiceImpl", HelloServiceImpl.class);
helloService.sayHello();
} catch (BeansException e) {
e.printStackTrace();
}
}
}
分享到:
相关推荐
spring_dubbo spring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_dubbospring_...
NULL 博文链接:https://diaochenlong2.iteye.com/blog/1831285
NULL 博文链接:https://listen-raining.iteye.com/blog/2128694
spring动态向容器中添加bean和删除指定bean,不需要重启应用
spring bean配置 运行环境:eclipse 构建工具:maven 不提供maven构建,maven用来解决jar包的依赖
“#simple_spring_bean”
spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean spring 获取bean
Spring Bean创建初始化流程
本篇文章主要介绍了Spring 中如何控制2个bean中的初始化顺序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
spring bean 的生命周期,把运行结果的日志,用sublime打开对比查看,你会有比较清晰的认识
14.尚硅谷_SpringCloud_SpringCloud国内使用情况 15.尚硅谷_SpringCloud_Rest微服务案例-总体概述 16.尚硅谷_SpringCloud_Rest微服务案例-父工程构建步骤 17.尚硅谷_SpringCloud_Rest微服务案例-API公共模块和部门...
Spring bean 一般通过配置文件和注解进行加载,如果要实现jar或class文件,动态实现spring bean 的动态加载,并通过UrlClassLoader完成jar和class文件的加载。可以实现jar的热替换。spring的bean动态加载则需要对...
spring运行过程中动态注册bean,代码实例
Spring動態加載Bean的實現 初學Spring者
Spring_AOP_学习小结 Spring_AOP_学习小结 Spring_AOP_学习小结 Spring_AOP_学习小结
spring自动扫描和管理Bean的示例
spring基于xml配置加载bean 的demo 了解spring的反射机制
在非spring注解类中使用spring容器中的bean_普通类中使用yml配置文件中的配置信息,在大数据采集项目中用到的,已经测试过了
spring bean 属性总结
bean文件 springbean文件