定义:
bean的定义、使用、销毁

其初始化最好在javabean中写出来,用一个Init()方法,而不是实现Spring的接口,这样独立多了。
如下:
bean的定义、使用、销毁
bean的定义、使用、销毁

使用:
在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">

相关文章:

  • 2021-11-01
  • 2021-10-20
  • 2021-12-27
  • 2022-12-23
  • 2022-03-08
  • 2022-12-23
  • 2021-10-06
  • 2021-08-13
猜你喜欢
  • 2022-12-23
  • 2022-02-13
  • 2022-12-23
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2021-05-14
相关资源
相似解决方案