1. 程式人生 > >spring ioc---DI進階之雜項(idref標籤;複合屬性;懶載入)

spring ioc---DI進階之雜項(idref標籤;複合屬性;懶載入)

雜項 說明
idref標籤 注入的是容器中例項bean的id值,型別是java.lang.String,而非bean例項!
attr1.attr2.attr3

採用巢狀屬性的方式,可直接設定多重依賴bean的屬性值.

前提是保證前置的屬性非null.否則報空指標異常.

lazy-init(bean的內建屬性) 例項化容器的時候,不初始化此bean,待使用的時期,容器才例項化
default-lazy-init="default"(beans的內建屬性)
設定全域性的懶載入機制,作用域覆蓋容器中的所有bean.

補充:(xsd文件中的說明)

  • idref的文件說明:

The id of another bean in this factory or an external factory (parent or included factory). While a regular 'value' element could instead be used for the same effect, using idref indicates that the Spring container should check that the value actually corresponds to a bean id.

  • lazy-init的文件說明

Indicates whether this bean is to be lazily initialized. If "false", it will be instantiated on startup by bean factories that perform eager initialization of singletons. The effective default is "false". Note: This attribute will not be inherited by child bean definitions. Hence, it needs to be specified per concrete bean definition. It can be shared through the 'default-lazy-init' attribute at the 'beans' level and potentially inherited from outer 'beans' defaults in case of nested 'beans' sections (e.g. with different profiles).

  • default-lazy-init的文件說明

The default 'lazy-init' value; see the documentation for the 'lazy-init' attribute of the 'bean' element. The default is "default", indicating inheritance from outer 'beans' sections in case of nesting, otherwise falling back to "false".​​​​​​​

 


具有依賴關係的類物件

package siye;
/**
 * 此類用來測試標籤idref和複合屬性的注入.
 */
public class Person
{
	private String genericName;
	public void setGenericName(String genericName)
	{
		this.genericName = genericName;
	}
	public String getGenericName()
	{
		return genericName;
	}
}
package siye;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
 * 此類用來測試標籤idref和複合屬性的注入.
 */
public class User implements ApplicationContextAware
{
	/**
	 * 以下屬性和行為用來測試標籤idref的注入.
	 */
	// 宣告字串,用來接收idref標籤注入的bean的id的值.
	private String beanId;
	// 宣告空容器類物件.
	private ApplicationContext applicationContext;
	// 屬性beanId的寫入器.
	public void setBeanId(String beanId)
	{
		this.beanId = beanId;
	}
	// 屬性beanId的讀取器.
	public String getBeanId()
	{
		return beanId;
	}
	// 內建一個容器例項,用來獲取新的例項Person.
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException
	{
		this.applicationContext = applicationContext;
	}
	// 獲取Person的例項,用來測試idref注入的bean的id的值.
	// 即容器中存在的合法的bean名稱.
	public Person getPersonInstance()
	{
		if (applicationContext != null)
		{
			return this.applicationContext.getBean(beanId, Person.class);
		}
		return null;
	}
	/*
	 * 以下屬性和行為用來測試複合屬性的注入.
	 */
	// 依賴Person類物件.
	private Person person;
	// 定義getter方法.
	public Person getPerson()
	{
		return person;
	}
	// 定義setter方法
	public void setPerson(Person person)
	{
		this.person = person;
	}
}

測試懶載入機制的類物件

package siye;
/**
 * 此類用來測試bean的懶載入機制
 */
public class TargetObj
{
	public void init()
	{// 打樁測試此類的例項化時機
		System.out.println(TargetObj.class.getName() + "被例項化了");
	}
}

配置檔案,config.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd"
	default-init-method="init" default-lazy-init="default">

	<!--測試懶載入 -->
	<bean class="siye.TargetObj" lazy-init="true" />

	<!--以下配置測試idref和複合屬性的注入 -->
	<bean id="person" class="siye.Person" scope="prototype" />

	<bean id="user" class="siye.User">
		<!--idref標籤注入String值,即bean的id值(bean名稱). -->
		<property name="beanId">
			<idref bean="person" />
		</property>
		<property name="person" ref="person" />
		<!--複合屬性的注入,保證內建的前置引用屬性非空!否則報空指標異常. -->
		<property name="person.genericName" value="xxx103號" />
	</bean>

</beans>

測試類

package siye;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UnitTest
{
	public static void main(String[] args)
	{
		String path = "classpath:/siye/config.xml";
		ClassPathXmlApplicationContext context =
				new ClassPathXmlApplicationContext(path);
		User obj = context.getBean(User.class);
		/**
		 * 測試idref標籤.
		 */
		// 獲取User類例項中beanId的值
		// 此值bean的id值,應當存在容器的bean例項中.
		System.out.println(obj.getBeanId());
		Person person = obj.getPersonInstance();
		System.out.println(person);
		/*
		 * 測試複合屬性
		 */
		System.out.println(person.getGenericName());
		System.out.println(obj.getPerson().getGenericName());
		/*
		 * 測試懶載入機制
		 */
		// context.getBean(TargetObj.class);
		context.close();
	}
}