Spring內部bean實例


在Spring框架中,一個bean僅用於一個特定的屬性,這是提醒其聲明為一個內部bean。內部bean支持setter注入“property”和構造器注入"constructor-arg“。
下面來看看一個詳細的例子,演示使用 Spring 內部 bean 。
package com.yiibai.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.yiibai.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 + "]";
	}	
}
很多時候,可以使用 'ref' 屬性來引用“Person” bean到“Customer” Bean,person的屬性如下:
<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.yiibai.common.Customer">
		<property name="person" ref="PersonBean" />
	</bean>

	<bean id="PersonBean" class="com.yiibai.common.Person">
		<property name="name" value="yiibai" />
		<property name="address" value="address1" />
		<property name="age" value="28" />
	</bean>
	
</beans>
在一般情況下,引用這樣也沒有問題,但由於“yiibai” persion bean 只用於Customer bean,這是更好地聲明 “yiibai” person 作為一個內部 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.yiibai.common.Customer">
		<property name="person">
			<bean class="com.yiibai.common.Person">
				<property name="name" value="yiibai" />
				<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.yiibai.common.Customer">
		<constructor-arg>
			<bean class="com.yiibai.common.Person">
				<property name="name" value="yiibai" />
				<property name="address" value="address1" />
				<property name="age" value="28" />
			</bean>
		</constructor-arg>
	</bean>
</beans>
注意:
id 或 name 值在bean類是沒有必要以一個內部 bean 呈現,它會簡單地忽略Spring容器。

執行結果:

package com.yiibai.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=yiibai]]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM