spring自动装配通俗易懂的解释零基础也能看懂

来源:互联网 发布:液压系统计算软件 编辑:程序博客网 时间:2024/05/01 14:02

Spring自动装配通俗易懂的解释

在应用中,我们常常使用<ref>标签为JavaBean注入它依赖的对象。但是对于一个大型的系统,这个操作将会耗费我们大量的资源,我们不得不花费大量的时间和精力用于创建和维护系统中的<ref>标签。实际上,这种方式也会在另一种形式上增加了应用程序的复杂性,那么如何解决这个问题呢?Spring为我们提供了一个自动装配的机制,尽管这种机制不是很完善,但是在应用中结合<ref>标签还是可以大大的减少我们的劳动强度。前面提到过,在定义Bean时,<bean>标签有一个autowire属性,我们可以通过指定它来让容器为受管JavaBean自动注入依赖对象。

 

<bean>autowire属性有如下六个取值,他们的说明如下:

 

1 No:即不启用自动装配。Autowire默认的值。

 

2 byName:通过属性的名字的方式查找JavaBean依赖的对象并为其注入。比如说类Computer有个属性printer,指定其autowire属性为byName后,Spring IoC容器会在配置文件中查找id/name属性为printerbean,然后使用Seter方法为其注入。

 

3 byType:通过属性的类型查找JavaBean依赖的对象并为其注入。比如类Computer有个属性printer,类型为Printer,那么,指定其autowire属性为byType后,Spring IoC容器会查找Class属性为Printerbean,使用Seter方法为其注入。

 

4 constructor:通byType一样,也是通过类型查找依赖对象。与byType的区别在于它不是使用Seter方法注入,而是使用构造子注入。

 

5 autodetect:在byTypeconstructor之间自动的选择注入方式。

 

6 default:由上级标签<beans>default-autowire属性确定。

 

注意:在配置bean时,<bean>标签中Autowire属性的优先级比其上级标签高,即是说,如果在上级标签中定义default-autowire属性为byName,而在<bean>中定义为byType时,Spring IoC容器会优先使用<bean>标签的配置。

 

下面通过一个例子来说明如何在应用中使用自动装配。新建一个java工程,为其添加上Spring开发能力。

需要用到的包spring.jar/commons-logging.jar

 

 

创建一个cn.csdn.test包,再分别创建学校类(School)、学生类(Student)和老师类(Teacher),为学校类添加Host类型的属性hostDisplay类型的属性display,再添加一个run方法,让学校可以“运行”一起来。属性代码如下:

 

学校类的代码:

package cn.csdn.test;

/**

*@author by crazyban||hotboy

*/

 

public class School {

    private Student student;

    private Teacher teacher;

   

    public void run(){

       System.out.println("我是学校系统");

       System.out.println("学生名:"+student.getName()+",");

       System.out.println("教师名:"+teacher.getName());

      

    }

   

   

    public Student getStudent() {

       return student;

    }

    public void setStudent(Student student) {

       this.student = student;

    }

    public Teacher getTeacher() {

       return teacher;

    }

    public void setTeacher(Teacher teacher) {

       this.teacher = teacher;

    }

   

}

学生类的代码:

package cn.csdn.test;

 

public class Student {

    private String name;

   

    public void setName(String name) {

       this.name = name;

    }

    public String getName() {

       return name;

    }

}

教师类的代码:

package cn.csdn.test;

 

public class Teacher {

    private String name;

   

    public void setName(String name) {

       this.name = name;

    }

    public String getName() {

       return name;

    }

   

}

下面是配置文件:

<?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 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"

        default-autowire="autodetect">

 

 

 

 

    <bean id="school" class="cn.csdn.test.School" autowire="byName"></bean>

    <bean id="school1" class="cn.csdn.test.School" autowire="byType"></bean>

    <bean id="school2" class="cn.csdn.test.School" autowire="default"></bean>

  

   

    <bean id="student" class="cn.csdn.test.Student">

       <property name="name">

           <value>crazy</value>

       </property>

    </bean>

    <bean id="teacher" class="cn.csdn.test.Teacher">

       <property name="name">

           <value>crazyban1</value>

       </property>

    </bean>

</beans>

 

测试类:

package cn.csdn.test;

 

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

 

 

 

public class App {

    @Test

    public void test(){

       ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:appl*.xml");

       School sl=(School) ac.getBean("school");

       sl.run();

    }

    @Test

    public void test1(){

       ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:appl*.xml");

       School sl=(School) ac.getBean("school1");

       sl.run();

    }

    @Test

    public void test2(){

       ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:appl*.xml");

       School sl=(School) ac.getBean("school2");

       sl.run();

    }

}

结果都是:

我是学校系统

学生名:crazy,

教师名:crazyban1

怎么样很容易懂吧,再见。

 

原创粉丝点击