spring中的init-method的不解
我有两个类都实现了Ijiaotong接口
代码如下
第一个类
package com.springtest;
public class Car implements Ijiaotong {
private String name;
private Ipeople ipeople;
public Ipeople getIpeople() {
return ipeople;
}
public void setIpeople(Ipeople ipeople) {
this.ipeople = ipeople;
}
public void init()
{
System.out.println("初始化");
}
public void kai()
{
ipeople.fly();
System.out.println( "开动");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
第二个类
package com.springtest;
public class Plane implements Ijiaotong {
private String name;
private Ipeople ipeople;
public Ipeople getIpeople() {
return ipeople;
}
public void setIpeople(Ipeople ipeople) {
this.ipeople = ipeople;
}
public String getName() {
return name;
}
public void init2()
{
System.out.println("初始化");
}
public void setName(String name) {
this.name = name;
}
public void kai() {
ipeople.fly();
System.out.println(name + "开动");
}
}
接口:
package com.springtest;
public interface Ijiaotong {
public void kai();
}
spring的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<bean id="ipeople" class="com.springtest.People"></bean>
<bean id="car" class="com.springtest.Car" init-method="init">
</bean>
<bean id="plane" class="com.springtest.Plane" init-method="init2">
</bean>
</beans>
程序的入口点类
package com.springtest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new FileSystemXmlApplicationContext(
"src/applicationContext.xml");
System.out.println(applicationContext);
Ijiaotong jiaotong = (Ijiaotong) applicationContext.getBean("car");
jiaotong.kai();
}
}
我不明白:我只是用spring实例化了一个car类的对象为什么两个类的init方法都调用了
输出结果:
初始化
初始化
人类
开动