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
最新推荐文章于 2025-10-27 11:50:33 发布
本文介绍了三种在Spring框架中实例化Bean的方法:通过BeanWrapper设置属性值、使用BeanFactory从配置文件中获取Bean以及利用ApplicationContext初始化上下文并获取Bean。这些方法展示了Spring管理对象生命周期的灵活性。
4914

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



