bean的自动装配
bean的自动装配
概述
自动装配是指Spring会自己在上下文中寻找并自动给bean装配属性
Spring有三种装配方式:
- 使用xml显式配置
- java显式配置
- 隐式地自动装配bean[重要]
本文主要讲隐式自动装配bean
实践
基本代码: 一人拥有一猫一狗
猫类
package com.kuangstudy;
/**
* 功能描述
*
* @since 2022-06-26
*/
public class Cat {
public void shout() {
System.out.println("miao~");
}
}
狗类
package com.kuangstudy;
/**
* 功能描述
*
* @since 2022-06-26
*/
public class Dog {
public void shout() {
System.out.println("wang~");
}
}
人类
package com.kuangstudy;
/**
* 功能描述
*
* @since 2022-06-26
*/
public class Person {
private String name;
private Cat cat;
private Dog dog;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", cat=" + cat +
", dog=" + dog +
'}';
}
}
测试类
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.kuangstudy.Person;
/**
* 功能描述
*
* @since 2022-06-26
*/
public class Test02 {
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Person person = applicationContext.getBean("xiaohong", Person.class);
person.getCat().shout();
person.getDog().shout();
}
}
装配配置
手动装配
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="com.kuangstudy.Cat"></bean>
<bean id="dog" class="com.kuangstudy.Dog"></bean>
<bean id="xiaohong" class="com.kuangstudy.Person">
<property name="cat" ref="cat"></property>
<property name="dog" ref="dog"></property>
</bean>
</beans>
自动装配byName
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="com.kuangstudy.Cat"></bean>
<bean id="dog" class="com.kuangstudy.Dog"></bean>
<bean id="xiaohong" class="com.kuangstudy.Person" autowire="byName"></bean>
</beans>
自动装配byType
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.kuangstudy.Cat"></bean>
<bean class="com.kuangstudy.Dog"></bean>
<bean id="xiaohong" class="com.kuangstudy.Person" autowire="byType"></bean>
</beans>
总结
手动装配需要给person类手动配上bean类型的属性
自动装配byName可以寻找跟属性相同名称的bean自动完成装配,要求与属性名称相同的bean必须存在且唯一
自动装配byType可以寻找类型相同的bean自动完成装配,要求与属性类型相同的bean唯一