人
public class People {
private Teacher teacher;
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "People [teacher=" + teacher + "]";
}
}
先生
public class Teacher {
}
applicaitonContext.xml
- 自動注入には2つの設定方法があります
- グローバルなものは属性default-beansタグのautowireです
- 属性がデフォルトまたはnoの場合、またはこの属性が構成されていない場合、グローバルデフォルトはnoです。
- 属性がbyNameの場合:メンバーオブジェクトと同じIDの自動インジェクションを見つけることを意味します
- 属性がbyTypeの場合:メンバーオブジェクトクラスとしてのクラスの自動挿入が見つかったことを示します。
- 属性がbyConstructor:の場合、メンバーオブジェクトを構築するためのメソッドがあり、メソッドのオブジェクトパラメータ名がBeanのIDと同じである場合、コンストラクタを介して自動注入が実行されます。
- 単一のラベルの属性default-autowireもあります
- 構成されていない場合、またはデフォルトとして構成されている場合は、グローバル構成に従って続行します
- 他のものを構成する場合は、グローバルな状況を無視し、ラベル構成に従って注入します
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="default"
>
<bean id="teacher" class="cn.wit.test.Teacher"></bean>
<bean id="people" class="cn.wit.test.People" autowire="byName">
</bean>
</beans>
テスト
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
People peo=ac.getBean("people",People.class);
System.out.println(peo);
}
}