Spring中的Bean配置 (9)—— XML 配置里的 Bean自动装配

在这里插入图片描述
Spring中的Bean配置 (1)——内容提要
Spring中的Bean配置 (2)—— IOC和DI
Spring中的Bean配置 (3)——在Spring的IOC容器里配置Bean(通过全类名(反射))——基于XML文件的方式
Spring中的Bean配置 (4)——依赖注入方式
Spring中的Bean配置 (5)——字面值
Spring中的Bean配置 (6)——引用其他Bean
Spring中的Bean配置 (7)——注入参数详解:null值和级联属性
Spring中的Bean配置 (8)—— 集合属性
Spring中的Bean配置 (9)—— XML 配置里的 Bean自动装配
Spring中的Bean配置 (10)—— 继承 Bean 配置和依赖 Bean 配置
Spring中的Bean配置 (11)——Bean的作用域
Spring中的Bean配置 (12)——使用外部属性文件
Spring中的Bean配置 (13)—— Spring表达式语言:SpEl
Spring中的Bean配置 (14)——IOC容器中Bean的生命周期
Spring中的Bean配置 (15)——在Spring的IOC容器里配置Bean(通过工厂方法创建Bean)——基于XML文件的方式
Spring中的Bean配置 (16)——在Spring的IOC容器里配置Bean(通过FactoryBean)——基于XML文件的方式

Spring中的Bean配置 (17)——在Spring的IOC容器里配置Bean——基于注解的方式来配置Bean

  • Spring IOC 容器可以自动装配 Bean. 需要做的仅仅是 bean autowire 属性里指定自动装配的模式
  • byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种情况下, Spring 将无法判定哪个 Bean 最合适该属性, 所以不能执行自动装配.
  • byName(根据名称自动装配): 必须将目标 Bean 的名称和属性名设置的完全相同.
  • constructor(通过构造器自动装配): 当 Bean 中存在多个构造器时, 此种自动装配方式将会很复杂. 不推荐****使用

代码演示

Car.java

package com.atguigu.spring;

public class Car {
	
	private String name;
	private String number;
	
	public Car(String name, String number) {
		
		super();
		this.name = name;
		this.number = number;
		
	}

	@Override
	public String toString() {
		
		return "Car [name=" + name + ", number=" + number + "]";
		
	}

}

Person.java

package com.atguigu.spring;

public class Person {
	
	private Address address;
	private Car car;
	
	public void setAddress(Address address) {
		
		this.address = address;
		
	}
	
	public void setCar(Car car) {
		
		this.car = car;
		
	}
	
	@Override
	public String toString() {
		
		return "Person [address=" + address + ", car=" + car + "]";
		
	}
	
}

Address.java

package com.atguigu.spring;

public class Address {
	
	private int address;
	private String location;
	
	public void setAddress(int address) {
		
		this.address = address;
		
	}
	
	public void setLocation(String location) {
		
		this.location = location;
		
	}
	@Override
	public String toString() {
		
		return "Address [address=" + address + ", location=" + location + "]";
		
	}
	
}

application.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">

<bean id="car" class="com.atguigu.spring.Car">
<constructor-arg value="baoma" type="java.lang.String"></constructor-arg>
<constructor-arg value="闽A7878" type="java.lang.String"></constructor-arg>
</bean>

<bean id="address" class="com.atguigu.spring.Address">
<property name="address" value="1234"></property>
<property name="location" value="fj"></property>
</bean>

<!-- 测试使用autowire属性自动装配 -->
<!-- byName根据bean的名字和当前的bean的setter风格的属性名进行自动装配 -->
<bean id="person1" class="com.atguigu.spring.Person" autowire="byName">
</bean>

<!-- byTypee根据bean的类型和当前的bean的属性的类型进行自动装配,若ioc容器有一个以上的类型匹配的bean则抛异常 -->
<bean id="person2" class="com.atguigu.spring.Person" autowire="byType">
</bean>

</beans>

Main.java

package com.atguigu.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {

		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
		
		Person person1=(Person)applicationContext.getBean("person1");
		Person person2=(Person)applicationContext.getBean("person2");
		
		System.out.println(person1);
		System.out.println(person2);

	}

}

但是自动装配也有缺点如下

  • 在 Bean 配置文件里设置 autowire 属性进行自动装配将会装配 Bean 的所有属性. 然而, 若只希望装配个别属性时, autowire 属性就不够灵活了.
  • autowire 属性要么根据类型自动装配, 要么根据名称自动装配, 不能两者兼而有之.
  • 一般情况下,在实际的项目中很少使用自动装配功能,因为和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力一些
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值