SpringIOC自动装配05

少使用,了解即可

c65302669f503a78aa2e0a94f243247330b.jpg

前面的bean全部是手动配置,现在来学习自动装配

一、default-autowire="byName"通过名称自动装配

如果头文件不写default-autowire,系统自动默认为default-autowire="default",就是不自动装配

369e828fc7ab0bf1007d5df628385251309.jpg

现在我们将bean.xml头文件改为default-autowire="byName",把手动注入的Dog删掉

<?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-autowire="byName">

	<bean id="dog" class="com.java.entity.Dog">
		<property name="name" value="Jack"></property>
	</bean>
	<bean id="dog2" class="com.java.entity.Dog">
		<property name="name" value="Tom"></property>
	</bean>
	
	<bean id="people1" class="com.java.entity.People">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
	</bean>

</beans>

Dog.java

package com.java.entity;

public class Dog {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}

People.java

这里的狗是Dog,不是Dog1,所以根据名称自动装配的时候People1装配Jack狗

package com.java.entity;


public class People {
	private int id;
	private int age;
	private String name;
	private Dog dog;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Dog getDog() {
		return dog;
	}
	public void setDog(Dog dog) {
		this.dog = dog;
	}
	@Override
	public String toString() {
		return "People [id=" + id + ", age=" + age + ", name=" + name + ", dog=" + dog.getName() + "]";
	}
	
	
	
	
	
}

测试:

T.java

package com.java.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.java.entity.People;

public class T {
	
	private ApplicationContext ac;
	
	@Before
	public void setUp() throws Exception {
		ac=new ClassPathXmlApplicationContext("beans.xml");
	}

	@Test
	public void test1() {
		People people=(People)ac.getBean("people1");
		System.out.println(people);
	}
	
}

结果:bean.xml没有手动注入,而是通过实体类属性名自动装配

4e5a3ad781e7fbc180f9a59414ecb14c339.jpg

当我将People.java修改为Dog2时

package com.java.entity;


public class People {
	private int id;
	private int age;
	private String name;
	private Dog dog2;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Dog getDog2() {
		return dog2;
	}
	public void setDog2(Dog dog2) {
		this.dog2 = dog2;
	}
	@Override
	public String toString() {
		return "People [id=" + id + ", age=" + age + ", name=" + name + ", dog2=" + dog2.getName() + "]";
	}
	
	
}

结果装配Tom狗

e4f18f1c5c69a60493364bfb66abfdaed7f.jpg

二、 default-autowire="byType通过类型自动匹配

改为byType自动装配时,因为People.java有private Dog dog;

所以bean.xm会自动装配一条狗进去

前面我们bean.xml如下:有dog和dog2两条狗,这样测试,people发现两个相同类型的bean却不懂注入哪个类型,所以会报错

<?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-autowire="byType">

	<bean id="dog" class="com.java.entity.Dog">
		<property name="name" value="Jack"></property>
	</bean>
	<bean id="dog2" class="com.java.entity.Dog">
		<property name="name" value="Tom"></property>
	</bean>
	
	<bean id="people1" class="com.java.entity.People">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
	</bean>

</beans>

4576147001734f91f269b717a283f88ed1e.jpg

所以设置根据Type自动装配时,只需要一条狗就行了

dag时:把dog2删除

617fa1ce26771fc9da98d1f90ef5dd7ab36.jpg

 

dag2时:把dog删除

cf0a33671f6430a0c4ff74bae33f2159fe4.jpg

 

三、default-autowire="constructor"构造方法注入

和type类似,只能一条狗

修改People的构造方法

package com.java1234.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class People {

	private int id;
	private String name;
	private int age;
	private Dog dog;
	
	
	
	public People() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
	public People(Dog dog) {
		super();
		System.out.println("constructor");
		this.dog = dog;
	}


	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Dog getDog() {
		return dog;
	}
	public void setDog(Dog dog) {
		this.dog = dog;
	}
	@Override
	public String toString() {
		return "People [id=" + id + ", name=" + name + ", age=" + age
				+ ", dog=" + dog.getName() + "]";
	}
	

}

bean.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-autowire="constructor">


	<bean id="dog2" class="com.java.entity.Dog">
		<property name="name" value="Tom"></property>
	</bean>
	
	<bean id="people1" class="com.java.entity.People">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
	</bean>

</beans>

结果:通过构造函数来找bean自动装配

db10467f3f8639339e3659065c3c4cd5a4c.jpg

转载于:https://my.oschina.net/u/3848699/blog/2244806

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值