Spring IOC注入(三)自动注入

前俩篇讲了一下IOC注入的set方式注入和基于构造器的注入,现在就来讲讲自动注入~

自动注入:容器依照一些规则去装配bean中的一个属性

注意:自动装配只对[对象类型]起作用,对基本类型不起作用.

自动注入有俩种方式:

第一种: 在beans标签中配置装载方式:defadefault-autowire="byType"或defult-autowire="byName"
                default-autowire="byType"或default-autowire="byName"
    在根元素beans中加入这个属性,那么下面所有的bean都会
    使用byName或byType的方式进行自动注入,如果在下面的某一个bean
    里面想使用其他的方式进行注入,可以用autowire=""属性进行
    说明,或者某一个bean不想使用任何自动注入就使用autowire="no"

第二种:在bean标签中指定配置方式

      autowire="byName":
    spring容器会到当前的类中找property的名字,然后
    再根据这个名字去spring容器中找有没有和这个property
    名字相同的对象,有的话,就把这个对象当做参数放到
    setXxxx这个方法里面注入进来.
    注意:了解property指的类中的什么东西。



      autowire="byType":
    spring容器会根据当前类中的set方法里面参数的类型,
    去容器中找相匹配的对象,如果没找到就算了,如果找到
    一个就注入进来,如果找到多个,那么就会报错了.


      autoWrite="constructor" 
根据构造器的参数类型去匹配

现在就分别来使用这俩种方式~

第一种:

(1)在beans标签中配置装载方式:defadefault-autowire="byType"

              在根元素beans中加入这个属性,那么下面所有的bean都会
     使用byType的方式进行自动注入,如果在下面的某一个bean
     里面想使用其他的方式进行注入,可以用autowire=""属性进行
     说明,或者某一个bean不想使用任何自动注入就使用autowire="no"

代码如下:

俩个Bean类--这是固定代码,后面是要一直使用的

Student类:

public class Student {

	private long id;

	private String name;

	private int age;

	public Student(long id, String name, int age) {

		this.id = id;

		this.name = name;

		this.age = age;

	}

	public Student() {
		System.out.println("in 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 int getAge() {

		return age;

	}

	public void setAge(int age) {

		this.age = age;

	}
}

Teacher类:

public class Teacher {

	private long id;

	private String name;

	//引用类型的属性
	private Student student;


	public Teacher() {

		// TODO Auto-generated constructor stub
	}

	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-3.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd"
          default-autowire="byType"
  >
     <bean name="student" class="com.x.spring.bean.Student">
     	<property name="id">
     		<value>1001</value>
     	</property>
     </bean>
     <!-- <bean name="student1" class="com.x.spring.bean.Student">
     	<property name="id">
     		<value>2002</value>
     	</property>
     </bean> -->
     <bean name="teacher" class="com.x.spring.bean.Teacher" >
     </bean> 
</beans>




当前类是Teacher类,可以从测试类看出,测试类我会在下面给出代码,当前set方法里里面对象参数的类型有:

Teacher类:


把那几行注释掉,Student类就可以注入进来了~

AutowriredTest测试类:

public class AutowriredTest {

	public static void main(String[] args) {
		String path = "autowired.xml";
		ApplicationContext container = 
				new ClassPathXmlApplicationContext(path);
		Teacher t = (Teacher)container.getBean("teacher");
		System.out.println(t);
		System.out.println(t.getStudent().getId());
	}
}


效果图:




好,那现在来看看beans标签的第二种用法~

(2)在beans标签中配置装载方式:default-autowire="byName"

               在根元素beans中加入这个属性,那么下面所有的bean都会
     使用byName的方式进行自动注入,如果在下面的某一个bean
     里面想使用其他的方式进行注入,可以用autowire=""属性进行
     说明,或者某一个bean不想使用任何自动注入就使用autowire="no"

只需要改动配置文件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-3.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd"
          default-autowire="byName"
  >

     <bean name="student" class="com.x.spring.bean.Student">
     	<property name="id">
     		<value>1001</value>
     	</property>
     </bean>
      <bean name="student1" class="com.x.spring.bean.Student">
     	<property name="id">
     		<value>2002</value>
     	</property>
     </bean> 
     <bean name="teacher" class="com.x.spring.bean.Teacher" >
     </bean> 
</beans>
我来解释一下,如图:





运行一下,效果如图:


好了,第二种用法就讲完了~

那就讲第三种用法吧~

(3)在beans标签中配置装载方式:default-autowire="byType"

在bean标签写autowire="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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd"
          default-autowire="byType"
  >
     <bean name="student" class="com.x.spring.bean.Student">
     	<property name="id">
     		<value>1001</value>
     	</property>
     </bean>
      <bean name="student1" class="com.x.spring.bean.Student">
     	<property name="id">
     		<value>2002</value>
     	</property>
     </bean>  
     <bean name="teacher" class="com.x.spring.bean.Teacher" autowire="byName">
     </bean> 
</beans>

其实稍微想一下就可以想清楚,这和第二种用法是一样的~因为在根元素beans中加入这个属性default-autowire="byType",那么下面所有的bean都会使用byType的方式进行自动注入,如果在下面的某一个bean里面想使用其他的方式进行注入,可以用autowire=""属性进行说明,或者某一个bean不想使用任何自动注入就使用autowire="no"

而且会有优先级,先考虑bean标签里面的~

so,第三种用法就讲完了,开始第二种方式吧~


第二种:在bean标签中指定配置方式

(1)autowire="byType"

     spring容器会根据当前类中的set方法里面参数的类型,
    去容器中找相匹配的对象,如果没找到就算了,如果找到
    一个就注入进来,如果找到多个,那么就会报错了.

只需要改变一下配置文件即可~

<?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-3.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd"
          
  >
     <bean name="student" class="com.x.spring.bean.Student">
     	<property name="id">
     		<value>1001</value>
     	</property>
     </bean>
   <!--    <bean name="student1" class="com.x.spring.bean.Student">
     	<property name="id">
     		<value>2002</value>
     	</property>
     </bean>  -->
     <bean name="teacher" class="com.x.spring.bean.Teacher" autowire="byType">
     </bean> 
</beans>
这里我就不解释了,跟第一种方式的第一个用法是一样的~效果图也是一样的~

(2) autowire="byName"
               spring容器会到当前的类中找property的名字,然后
    再根据这个名字去spring容器中找有没有和这个property
    名字相同的对象,有的话,就把这个对象当做参数放到
    setXxxx这个方法里面注入进来.
    注意:了解property指的类中的什么东西。

也是只需要改变一下配置文件~

<?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-3.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd"
          
  >
     <bean name="student" class="com.x.spring.bean.Student">
     	<property name="id">
     		<value>1001</value>
     	</property>
     </bean>
      <bean name="student1" class="com.x.spring.bean.Student">
     	<property name="id">
     		<value>2002</value>
     	</property>
     </bean>  
     <bean name="teacher" class="com.x.spring.bean.Teacher" autowire="byName">
     </bean> 
</beans>
这个解释和效果图是跟第一种方式的第二个用法是一样的~

(3)autoWrite="constructor" 

这个不怎么用,我就不讲这个了~



好了,那就讲完了~

有什么疑问的可以问我大笑




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值