2つのエンティティ
パッケージcom.java.test4。 / ** * @author nidegui * @Create 2019年6月22日14時45分 * / publicクラスの人々 { プライベート整数ID。 プライベート文字列名; プライベート文字列の年齢; 民間の犬の犬。 公共の整数のgetId(){ IDを返します。 } パブリック人(){ } 公共犬getDog(){ 戻り犬; } 公共ボイドsetDog(犬犬){ this.dog =犬。 } 公共ボイドSETID(整数ID){ this.id = ID。 } パブリック文字列のgetName(){ 戻り名。 } 公共ボイドのsetName(文字列名){ this.name =名。 } パブリック文字列getAge(){ 戻り年齢; } 公共ボイドsetAge(文字列の年齢){ this.age =年齢。 } @Override 公共の文字列のtoString(){ リターン"人々 {" + "ID =" + ID + "名= '" +名+ '\'' + "年齢= '" +年齢+ '\'' + 「犬=」+犬+ 『}』。 } 公共人(整数ID、文字列名、文字列の年齢){ this.id = ID。 この。年齢=年齢; } }
パッケージcom.java.test4。 / ** * @author nidegui * @Create 2019年6月22日午後03時37分 * / publicクラス犬{ プライベート整数ID。 プライベート文字列名; 公共の整数のgetId(){ IDを返します。 } 公共ボイドSETID(整数ID){ this.id = ID。 } パブリック文字列のgetName(){ 戻り名。 } 公共ボイドのsetName(文字列名){ this.name =名。 } @Override パブリック文字列のtoString(){ リターン"{名前=" +名+ "}"。 } }
beans.xmlの
<?xml version = "1.0"エンコード= "UTF-8"?> <豆のxmlns = "http://www.springframework.org/schema/beans" のxmlns:XSI = "http://www.w3.org / 2001 / XMLスキーマ・インスタンス」 のxsi:のschemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <ビーンID = "DOG1"クラス= "com.java.test4.Dog"> <プロパティ名= "名前"値= "javk"> </ property>の </豆> <豆ID = "人"クラス= "com.java。 test4.People "> <プロパティ名="名前」値= "nidegui"> </ property>の <プロパティ名= "ID"値= "1"> </ property>の <プロパティ名= "年齢"値= "12"> </ property>の <プロパティ名= "犬" REF = "DOG1"> </ property>の </豆> </豆>
テスト:
パッケージcom.java.test4。 輸入org.springframework.context.ApplicationContext; 輸入org.springframework.context.support.ClassPathXmlApplicationContext; / ** * @author nidegui * @Create 2019年6月22日14時47 * / パブリッククラスTest { パブリック静的無効メイン(文字列[] args){ ApplicationContextの交流が=新しいClassPathXmlApplicationContext( "beans.xmlの")。 人の人々 =(ピープル)ac.getBean( "人")。 System.out.println(人)。 } }