1、使用BeanWrapper
HelloWorld hw=new HelloWorld();
BeanWrapper bw=new BeanWrapperImpl(hw);
bw.setPropertyvalue(”msg”,”HelloWorld”);
system.out.println(bw.getPropertyCalue(”msg”));
2、使用BeanFactory
InputStream is=new FileInputStream(”config.xml”);
XmlBeanFactory factory=new XmlBeanFactory(is);
HelloWorld hw=(HelloWorld) factory.getBean(”HelloWorld”);
system.out.println(hw.getMsg());
3、使用ApplicationConttext
ApplicationContext actx=new FleSystemXmlApplicationContext(”config.xml”);
HelloWorld hw=(HelloWorld) actx.getBean(”HelloWorld”);
System.out.println(hw.getMsg());
本文详细介绍了在Spring框架中使用三种不同方法实例化Bean的过程:通过BeanWrapper手动设置属性值;利用BeanFactory从配置文件加载Bean;以及借助ApplicationContext进行更高级的Bean管理。这些方法展示了Spring框架的强大灵活性和对不同场景的支持。
793

被折叠的 条评论
为什么被折叠?



