6.spring的各种IOC注入方式3——自动注入

实体类Teacher

public class Teacher {
	private long id;
	private String name;
	private Student student;
	public Teacher() {}
	public Teacher(Student student) {
		this.student = student;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
}

autowired.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.xsd"> 
	<!-- 手动注入 -->
	<bean name="stu" class="com.briup.bean.Student">
		<property name="id" value="1"></property>
		<property name="name" value="tom"></property>
		<property name="age" value="20"></property>
	</bean>
	<bean name="t" class="com.briup.bean.Teacher">
		<property name="id" value="1"></property>
		<property name="name" value="jack"></property>
		<!--在teacher中手动注入stu  注释掉拿到的是null-->
		<!-- <property name="student" ref="stu"></property> -->
	</bean>
</beans>

测试类

@Test
public void ioc_autowired() {
	try {
		String path = "com/briup/ioc/autowired/autowired.xml";
		ApplicationContext container = 
				new ClassPathXmlApplicationContext(path);
		Teacher t = (Teacher) container.getBean("t");
		System.out.println(t);
		System.out.println(t.getId());
		System.out.println(t.getName());
		System.out.println(t.getStudent());
	} catch (Exception e) {
		e.printStackTrace();
	}
}

结果getStudent是空的
在这里插入图片描述

自动注入的方式

1.autowire=“byName”:

spring容器会到当前的类中找property的名字,然后再根据这个名
字去spring容器中找有没有和这个property名字相同的对象,有的话,
就把这个对象当做参数放到setXxxx这个方法里面注入进来
bean name="student"是固定与实体类中的setStudent()相对应的,
不能是其他的
public void setStudent(Student student) {
	this.student = student;
}
<?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.xsd">
	<!-- 与setStudent对应 -->
	<bean name="student" class="com.briup.bean.Student">
		<property name="id" value="1"></property>
		<property name="name" value="tom"></property>
		<property name="age" value="20"></property>
	</bean>
	<!-- 自动注入student -->
	<bean name="t" class="com.briup.bean.Teacher" autowire="byName">
		<property name="id" value="1"></property>
		<property name="name" value="jack"></property>
	</bean>
</beans>

结果:
在这里插入图片描述

2.autowire=“byType”:

spring容器会根据当前类中的set方法里面参数的类型,
去容器中找相匹配的对象,如果没找到就算了,如果找到一个就注入进来,
如果找到多个,那么就会报错了
bean name="stu",不管name="名称",只要匹配到名称是Student类型就行了
<?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.xsd">
	<bean name="stu" class="com.briup.bean.Student">
    	<property name="id" value="1"></property>
    	<property name="name" value="tom"></property>
    	<property name="age" value="20"></property>
    </bean>
    <bean name="t" class="com.briup.bean.Teacher" autowire="byType">
    	<property name="id" value="1"></property>
    	<property name="name" value="jack"></property>
    </bean>
</beans>

3.default-autowire=“byName”,default-autowire=“byType”

了解即可,不常用

default-autowire="byName"
在根元素beans中加入这个属性,那么下面所有的bean都会使用byName的方式进行
自动注入,
如果在下面的某一个bean里面想使用其他的方式进行注入,可以用autowire=""
属性进行说明,或者某一个bean不想使用任何自动注入就使用autowire="no"
<?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.xsd"
           default-autowire="byName">
	<bean name="stu" class="com.briup.bean.Student">
		
	</bean> 
</beans>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值