Spring 学习笔记《依赖注入》—— 自动注入

Spring 学习笔记

自动注入

属性 autowire 用来设置自动注入的规则,与现有的 bean 匹配,对的上就帮你塞进去。一共5个值。
在这里插入图片描述|

  • byName : 按属性名 (就用它不用想)
  • byType : 按属性类型
  • constructor : 按构造器的形参类型和现有 bean 匹配(听着就不靠谱)
  • default : 按全局属性 (根节点 beansdefault-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=小学一年级]]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑虾

多情黯叹痴情癫。情癫苦笑多情难

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值