Spring 的 init-method 和 destory-method

关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种:

第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
第二种是:通过 在xml中定义init-method 和  destory-method方法
第三种是: 通过bean实现InitializingBean和 DisposableBean接口


spring中的init-method的不解

07-25

我有两个类都实现了Ijiaotong接口rn代码如下rn第一个类rnpackage com.springtest;rnrnpublic class Car implements Ijiaotong rn private String name;rn private Ipeople ipeople;rn public Ipeople getIpeople() rn return ipeople;rn rn public void setIpeople(Ipeople ipeople) rn this.ipeople = ipeople;rn rn public void init()rn rn System.out.println("初始化");rn rnrn public void kai()rn rn ipeople.fly();rn System.out.println( "开动");rn rn public String getName() rn return name;rn rn public void setName(String name) rn this.name = name;rn rnrn第二个类rnpackage com.springtest;rnrnpublic class Plane implements Ijiaotong rn private String name;rn private Ipeople ipeople;rn rn public Ipeople getIpeople() rn return ipeople;rn rn public void setIpeople(Ipeople ipeople) rn this.ipeople = ipeople;rn rn public String getName() rn return name;rn rn public void init2()rn rn System.out.println("初始化");rn rnrn public void setName(String name) rn this.name = name;rn rnrnrn public void kai() rn ipeople.fly();rn System.out.println(name + "开动"); rn rnrnrn接口:rnpackage com.springtest;rnrnpublic interface Ijiaotong rnrn public void kai();rn rnrnrnspring的配置文件rnrnrnrnrn rn rn rn rn rn rnrn程序的入口点类rnpackage com.springtest;rnrnimport org.springframework.context.ApplicationContext;rnimport org.springframework.context.support.FileSystemXmlApplicationContext;rnrnpublic class Test rn public static void main(String[] args) rn ApplicationContext applicationContext = new FileSystemXmlApplicationContext(rn "src/applicationContext.xml");rn System.out.println(applicationContext);rn Ijiaotong jiaotong = (Ijiaotong) applicationContext.getBean("car");rn jiaotong.kai();rn rn rnrn我不明白:我只是用spring实例化了一个car类的对象为什么两个类的init方法都调用了rnrn输出结果:rnrn初始化rn初始化rn人类rn开动rnrnrn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭