定义:
其初始化最好在javabean中写出来,用一个Init()方法,而不是实现Spring的接口,这样独立多了。
如下:
使用:
在Spring中有三种使用方式:
方法一,使用BeanWrapper:
HelloWorld helloWorld=new HelloWorld();
BeanWrapper bw=new BeanWrapperImpl(helloWorld);
bw.setPropertyValue("msg","HelloWorld");
System.out.println(bw.getPropertyValue("msg"));
方法二,使用BeanFactory:
InputSteam is=new FileInputStream("config.xml");
XmlBeanFactory factory=new XmlBeanFactory(is);
HelloWorld helloWorld=(HelloWorld) factory.getBean("helloWorld");
System.out.println(helloWorld.getMsg());
方法三,使用ApplicationContext:
ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml");
HelloWorld helloWorld=(HelloWorld)actx.getBean("HelloWorld");
System.out.println(helloWorld.getMsg());
销毁的方法:
在JavaBean 中写一个方法:
public void cleanup()
{
this.msg="";
this.date=null;
}
配置文件config.xml中
<bean id="HelloWorld" class="com.gc.HelloWorld" init-method="init" destroy-method="cleanup">