`
zhukang0725
  • 浏览: 15351 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

Spring中Bean的生命周期2

阅读更多
在前面我已经给出了一种使用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);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics