spring ioc---DI进阶之杂项(idref标签;复合属性;懒加载)

版权声明:仅供学习交流使用 https://blog.csdn.net/drxRose/article/details/84942269
杂项 说明
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();
	}
}

猜你喜欢

转载自blog.csdn.net/drxRose/article/details/84942269