Spring 学习笔记
- Spring 学习笔记《准备工作》
- Spring 学习笔记《依赖注入》—— 配置 applicationContext.xml
- Spring 学习笔记《依赖注入》—— 扫描配置文件注入
- Spring 学习笔记《依赖注入》—— 自动注入
自动注入
属性 autowire
用来设置自动注入的规则,与现有的 bean
匹配,对的上就帮你塞进去。一共5个值。
|
- byName : 按属性名 (就用它不用想)
- byType : 按属性类型
- constructor : 按构造器的形参类型和现有 bean 匹配(听着就不靠谱)
- default : 按全局属性 (根节点
beans
的default-autowire="byName"
属性)处理 - no : 不自动注入
/SpringIoC/src/applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd"
default-autowire="byName"
>
<!-- ************* Spring容器创建对象是通过定义<bean>标签来实现的 ************* -->
<!-- 上面学会了创建对象。下面讲解怎么注入。因为构造器创建对象也可注入值,但是不如 set 灵活。
所以一般我们都是先调用无参构造创建对象,然后 set 属性值 -->
<bean id="textbook" class="com.jerry.entity.Textbook" >
<property name="name" value="思想品德"></property>
<property name="level" value="小学二年级"></property>
</bean>
<!-- 自动注入 -->
<!-- byName -->
<bean id="teacherDIByName" class="com.jerry.entity.Teacher" autowire="byName"></bean>
<!-- byType :因为按类型来找,如果同一类型的 bean 不止一个,就会出翔 -->
<bean id="teacherDIByType" class="com.jerry.entity.Teacher" autowire="byType"></bean>
<!-- constructor :按构造器形参类型与 bean 匹配 -->
<bean id="teacherDiConstructor" class="com.jerry.entity.Teacher" autowire="constructor"></bean>
</beans>
/SpringIoC/src/com/jerry/test/TestSpringIoC.java
package com.jerry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jerry.entity.MyCollection;
import com.jerry.entity.Student;
import com.jerry.entity.Teacher;
import com.jerry.entity.Textbook;
public class TestSpringIoC {
public static void main(String[] args) {
// 获取Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// byName
Teacher teacherDIByName= (Teacher)context.getBean("teacherDIByName");// 从容器中获取对象
System.out.println("byName 自动注入,创建 bean : " + teacherDIByName.toString());// 查看结果
// byType :因为按类型来找,如果同一类型的 bean 不止一个,就会出翔
Teacher teacherDIByType= (Teacher)context.getBean("teacherDIByType");// 从容器中获取对象
System.out.println("byType 自动注入,创建 bean : " + teacherDIByType.toString());// 查看结果
// constructor :按构造器形参类型与 bean 匹配
Teacher teacherDiConstructor= (Teacher)context.getBean("teacherDiConstructor");// 从容器中获取对象
System.out.println("constructor 自动注入 : " + teacherDiConstructor.toString());// 查看结果
}
}
我以为 constructor自动
的 textbook
应该有值,但是并没有。
byName自动,创建 bean : Teacher [name=李老师, age=27, adder=鸟不拉屎村, tel=10086, salary=5000.0, textbook=课本 [name=思想品德, level=小学二年级]]
byType自动,创建 bean : Teacher [name=李老师, age=27, adder=鸟不拉屎村, tel=10086, salary=5000.0, textbook=课本 [name=思想品德, level=小学二年级]]
constructor自动,创建 bean : Teacher [name=赵老师, age=99, adder=太远太远村, tel=10010, salary=9999.0, textbook=null]
因为我在 Teacher
的构造函数里竟然没有赋值。加上 this.textbook = textbook;
就好了。真是忙糊涂了
// textbook 注入进来了,但是我没赋值
public Teacher(Textbook textbook){
this.name = "赵老师";
this.age = 99;
this.adder = "太远太远村";
this.tel = "10010";
this.salary = 9999d;
}
// 加上 this.textbook = textbook; 就好了
public Teacher(Textbook textbook){
this.name = "赵老师";
this.age = 99;
this.adder = "太远太远村";
this.tel = "10010";
this.salary = 9999d;
this.textbook = textbook;
}
看看效果
byName 自动注入,创建 bean : Teacher [name=李老师, age=27, adder=鸟不拉屎村, tel=10086, salary=5000.0, textbook=课本 [name=语文课本, level=小学一年级]]
byType 自动注入,创建 bean : Teacher [name=李老师, age=27, adder=鸟不拉屎村, tel=10086, salary=5000.0, textbook=课本 [name=语文课本, level=小学一年级]]
constructor 自动注入 : Teacher [name=赵老师, age=99, adder=太远太远村, tel=10010, salary=9999.0, textbook=课本 [name=语文课本, level=小学一年级]]