Spring内部のBeanの例
Springのフレームワークでは、特定の1つのプロパティに対してのみBeanが使用されるときは常に、それを内部Beanとして宣言することをお勧めします。内部beanは、セッターインジェクションの
property
とコンストラクターインジェクション
constructor-arg
の両方でサポートされています。
Spring内部Beanの使用方法を示すための詳細な例を参照してください。
package com.mkyong.common; public class Customer { private Person person; public Customer(Person person) { this.person = person; } public void setPerson(Person person) { this.person = person; } @Override public String toString() { return "Customer[person=" + person + "]"; } }
package com.mkyong.common; public class Person { private String name; private String address; private int age; //getter and setter methods @Override public String toString() { return "Person[address=" + address + ", age=" + age + ", name=" + name + "]"; } }
多くの場合、 “Person” Beanを以下のように “Customer” bean、personプロパティに参照するために
` ref
‘属性を使用することができます:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="CustomerBean" class="com.mkyong.common.Customer"> <property name="person" ref="PersonBean"/> </bean> <bean id="PersonBean" class="com.mkyong.common.Person"> <property name="name" value="mkyong"/> <property name="address" value="address1"/> <property name="age" value="28"/> </bean> </beans>
一般的に、このように参照するのは問題ありませんが、 ‘mkyong’の人のBeanはCustomer Beanにのみ使われるので、この ‘mkyong’の人を以下のように内部のBeanとして宣言する方が良いでしょう:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="CustomerBean" class="com.mkyong.common.Customer"> <property name="person"> <bean class="com.mkyong.common.Person"> <property name="name" value="mkyong"/> <property name="address" value="address1"/> <property name="age" value="28"/> </bean> </property> </bean> </beans>
この内部Beanは、次のようにコンストラクタインジェクションでもサポートされました。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="CustomerBean" class="com.mkyong.common.Customer"> <constructor-arg> <bean class="com.mkyong.common.Person"> <property name="name" value="mkyong"/> <property name="address" value="address1"/> <property name="age" value="28"/> </bean> </constructor-arg> </bean> </beans>
それを実行します
package com.mkyong.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[]args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"Spring-Customer.xml"}); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); } }
出力
Customer[person=Person[address=address1, age=28, name=mkyong]].... link://tag/spring/[spring]