spring ioc---DI进阶之引入依赖关系不明显的bean

版权声明:仅供学习交流使用 https://blog.csdn.net/drxRose/article/details/84933953

1,需要在bean标签中使用属性`depends-on`.

简言之,在使用bean之前,被标注的依赖bean要先于初始化.虽然两个bean之间的依赖关系不是清晰明显且直接的.

官方xsd中关于depends-on的说明

文档地址:http://www.springframework.org/schema/beans/spring-beans.xsd

The names of the beans that this bean depends on being initialized. The bean factory will guarantee that these beans get initialized before this bean. Note that dependencies are normally expressed through bean properties or constructor arguments. This property should just be necessary for other kinds of dependencies like statics (*ugh*) or database preparation on startup. Note: This attribute will not be inherited by child bean definitions. Hence, it needs to be specified per concrete bean definition.

2,使用context包中的注解@DependsOn.

源码文档说明:

Beans on which the current bean depends. Any beans specified are guaranteed to be created by the container before this bean. Used infrequently in cases where a bean does not explicitly depend on another through properties or constructor arguments, but rather depends on the side effects of another bean's initialization.

记录:第二次更新修改---121218


存在依赖关系的类对象

package siye;

import org.springframework.stereotype.Component;

@Component("perObj")
public class Person
{
	int num;
}
package siye;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

@Component("userObj")
@DependsOn("utilObj")
public class User
{

	@Autowired
	@Qualifier("perObj")
	Person person;
	public User(Person person)
	{
		this.person = person;
	}
}
package siye;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component("utilObj")
@Lazy
public class Util
{
	@Autowired
	@Qualifier("perObj")
	Person person;
	public Util(Person person)
	{
		this.person = person;
		person.num = 12;
	}
}

xml配置文件,文件名`config.xml`

<!--测试注解dependsOn的时候,需要将配置bean给注释掉. -->                         
<!-- <bean id="person" class="siye.Person" /> -->               
                                                                
<!-- <bean id="util" class="siye.Util" lazy-init="true"> -->    
<!-- <constructor-arg name="person" ref="person" /> -->         
<!-- </bean> -->                                                
                                                                
<!-- <bean id="user" class="siye.User" depends-on="util"> -->   
<!-- <constructor-arg name="person" ref="person" /> -->         
<!-- </bean> -->                                                
                                                                
<context:component-scan base-package="siye" />                  

测试类

package siye;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UnitTest
{

	private ClassPathXmlApplicationContext context;

	@Before
	public void init()
	{
		String path = "classpath:/siye/config.xml";
		context = new ClassPathXmlApplicationContext(path);

	}

	@Test
	public void testByXml()
	{
		/*
		 * 关于User的bean定义中,若不使用depends-on属性.
		 * 则num的值是0,使用的话,是12.
		 */
		User user = context.getBean("user", User.class);
		System.out.println(user.person.num);
	}


	@Test
	public void testByAnno()
	{
		User user = context.getBean("userObj", User.class);
		System.out.println(user.person.num);
	}


	@After
	public void destroy()
	{
		if (context != null)
		{
			context.close();
		}
	}
}

猜你喜欢

转载自blog.csdn.net/drxRose/article/details/84933953
今日推荐