在前面我已经给出了一种使用Bean的方法:
package com.jnotnull;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Test {
public static void main(String []args){
ApplicationContext context = new FileSystemXmlApplicationContext("config.xml");
HelloWorld helloworld = (HelloWorld)context.getBean("HelloWorld");
System.out.println(helloworld.getMessage());
}
}
这里用到的式ApplicationContext方式。
下面给出另外两种方式:
BeanWrapper和BeanFactory;
首先看看BeanWrapper:
HelloWorld helloworld=new HelloWorld();
BeanWrapper wrapper = new BeanWrapperImpl(helloworld);
wrapper.setPropertyValue("message","HelloWorld");
System.out.println(wrapper.getPropertyValue("message"));
下面使用BeanFactory
InputStream is = new FileInputSystem("config.xml");
XmlBeanFactory factory = new XmlBeanFactory(is);
HelloWorld helloworld = (HelloWorld)factory.getBean("HelloWorld");
System.out.println(helloworld.getMessage);
分享到:
相关推荐
spring bean 的生命周期,把运行结果的日志,用sublime打开对比查看,你会有比较清晰的认识
Spring bean生命周期demo
SpringBean的生命周期.mdj
Springbean生命周期
这个工程主要实现了: Spring中Bean的生命周期 applicationcontext的应用(实现国际化,事件的传递)
此资源是我的博客bean的生命周期的测试代码,只有源代码,没有相关库文件,环境是spring4.2 ,
介绍了Spring的Bean周期,容器周期,工厂周期,运行代码后可以看到运行结果
主要给大家介绍了Spring中Bean的生命周期和作用域及实现方式的相关资料,文中介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
Spring Bean 的生命周期在整个 Spring 中占有很重要的位置,掌握这些可以加深对 Spring 的理解。这篇文章主要介绍了Spring Bean 生命周期,需要的朋友可以参考下
四份资料介绍Spring Bean 的生命周期,从认识,熟悉,深究,记忆Spring Bean。希望这份资料能帮助你!
NULL 博文链接:https://cici9898-007.iteye.com/blog/1407695
一般情况下,我们只是关心如何正确地将Bean装配到容器中,并不关心Ioc容器是如何装配和销毁Bean的过程。但是恰恰有时候,我们需要自定义初始化或销毁Bean的过程,以满足一些“特殊的”需求。比如,数据源在关闭的...
今天小编就为大家分享一篇关于spring中bean的生命周期详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
2、alias用于定义Bean别名的; 3、import用于导入其他配置文件的Bean定义,这是为了加载多个配置文件,当然也可以把这些配置文件构造为一个数组(new String[] {“config1.xml”, config2.xml})传给...
Spring的生命周期是指实例化Bean时所经历的一系列阶段,即通过getBean()获取bean对象及设置对象属性时,Spring框架做了哪些事。Bean的生命周期从Spring容器实例化Bean到销毁Bean。 本文分别对 BeanFactory 和 ...
Spring管理的Bean的生命周期
Spring Bean 生命周期之“我从哪里来?”
学习Spring过程中,使用Eclipse调试Spring源码的关键断点文件。
本篇文章主要介绍了深入理解Spring中bean的生命周期介绍,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧