spring ioc---DI进阶之自动装配

版权声明:仅供学习交流使用 https://blog.csdn.net/drxRose/article/details/84935225
模式 说明
no/default (Default) No autowiring. Bean references must be defined via a ref element. Changing the default setting is not recommended for larger deployments, because specifying collaborators explicitly gives greater control and clarity. To some extent, it documents the structure of a system.
byName Autowiring by property name. Spring looks for a bean with the same name as the propertythat needs to be autowired. For example, if a bean definition is set to autowire by name, and it contains a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition named master, and uses it to set the property.
byType Allows a property to be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set.
constructor Analogous to byType, but applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.

自动装配有些局限性,在项目使用的时候,建议不要经常使用.以下是官方文档(版本4.3.20)中,对其局限性的描述.

Consider the limitations and disadvantages of autowiring:

  • Explicit dependencies in property and constructor-arg settings always override autowiring. You cannot autowire so-called simple properties such as primitives, Strings, and Classes (and arrays of such simple properties). This limitation is by-design.
  • Autowiring is less exact than explicit wiring. Although, as noted in the above table, Spring is careful to avoid guessing in case of ambiguity that might have unexpected results, the relationships between your Spring-managed objects are no longer documented explicitly.
  • Wiring information may not be available to tools that may generate documentation from a Spring container.
  • Multiple bean definitions within the container may match the type specified by the setter method or constructor argument to be autowired. For arrays, collections, or Maps, this is not necessarily a problem. However for dependencies that expect a single value, this ambiguity is not arbitrarily resolved. If no unique bean definition is available, an exception is thrown.
     

具有依赖关系的类对象

package siye;
public class Person
{
}
package siye;
public class User
{
	Person person;
	public void setPerson(Person person)
	{
		this.person = person;
	}
}

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

<bean class="siye.Person" />                           
<bean id="user" class="siye.User" autowire="byType" /> 

测试类

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 user = context.getBean("user", User.class);
		System.out.println(user.person);
		context.close();
	}
}

猜你喜欢

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