Spring 专题
专题目录
您的位置:java > Spring专题 > Spring内部bean实例
Spring内部bean实例
作者:--    发布时间:2019-11-20
在spring框架中,一个bean仅用于一个特定的属性,这是提醒其声明为一个内部bean。内部bean支持setter注入“property”和构造器注入"constructor-arg“。
下面来看看一个详细的例子,演示使用 spring 内部 bean 。
package com.h3.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.h3.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.h3.common.customer">
		<property name="person" ref="personbean" />
	</bean>

	<bean id="personbean" class="com.h3.common.person">
		<property name="name" value="h3" />
		<property name="address" value="address1" />
		<property name="age" value="28" />
	</bean>
	
</beans>
在一般情况下,引用这样也没有问题,但由于“h3” persion bean 只用于customer bean,这是更好地声明 “h3” 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.h3.common.customer">
		<property name="person">
			<bean class="com.h3.common.person">
				<property name="name" value="h3" />
				<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.h3.common.customer">
		<constructor-arg>
			<bean class="com.h3.common.person">
				<property name="name" value="h3" />
				<property name="address" value="address1" />
				<property name="age" value="28" />
			</bean>
		</constructor-arg>
	</bean>
</beans>
注意:
id 或 name 值在bean类是没有必要以一个内部 bean 呈现,它会简单地忽略spring容器。

执行结果:

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


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册