Spring bean自动装配的属性值注入的个人见解

来源:互联网 发布:eia数据美国公布时间 编辑:程序博客网 时间:2024/06/12 20:59

    Spring框架是现在开发使用比较流行的框架,主要的核心思想就是依赖注入(DI)和面向界面(AOP),依赖注入使得对频繁对象的创建,使用、销毁等步骤脱离出来,统一放入Spring的IOC容器,由Spring的IOC容器统一管理项目中常用的类,从而将程序员从繁琐的对象关系处理中解放出来,下面分享一下个人学习的Spring的部分心得,希望对各位学习spring的同学有所启发,关于AOP和DI的源码实现后续下次给大家分享......


House类:只有一个People属性,验证引用的ref引用bean的set方法注入方式

package com.zhiwei.autowire;

public class House {

    private People people;

    public People getPeople() {
        return people;
    }

    public void setPeople(People people){
        this.people=people;
    }

    @Override
    public String toString() {
        return "House [people=" + people + "]";
    }
}


People类:存在一个name和Dog属性,主要时验证普通的set方法的值注入

package com.zhiwei.autowire;
/**People类:属性:name,dog*/
public class People {

    private String name;
    private Dog dog;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        System.out.println("People is running setting.......");
        this.name = name;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    
    @Override
    public String toString() {
        return "People [name=" + name + ", dog=" + dog + "]";
    }
}



dog:一个属性:dogName:测试构造方法属性值的注入

package com.zhiwei.autowire;

public class Dog {

    private String dogName;

    public String getDogName() {
        return dogName;
    }

    public void setDogName(String dogName) {
        this.dogName = dogName;
    }
    
    public Dog(String dogName) {
        System.out.println("Dog is initting by coustructor..........");
        this.dogName = dogName;
    }

    @Override
    public String toString() {
        return "Dog [dogName=" + dogName + "]";
    }
}



Spring的配置文件: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"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
                <!-- default-autowire="autodetect" 设置默认的装配方式:default检测 -->

<!-- 构造函数注入dogName属性 -->
<bean id="dog" class="com.zhiwei.autowire.Dog">
<constructor-arg index="0" value="xiaohua"/>
</bean>

<!-- 通过普通属性注入name属性:set
  byName:people属性dog,如果IOC容器里面存在id=dog的对象则自动关联(注意类型必须匹配)
  byType:people的属性dog,如果IOC容器存在和dog一致的bean则自动关联(不允许存在多个目标相同类型的bean)
 -->
<bean id="people" class="com.zhiwei.autowire.People" autowire="byName">
<property name="name" value="squirrel"/>
</bean>

<!-- 通过set方法注入people属性 -->
<bean id="house" class="com.zhiwei.autowire.House">
<property name="people" ref="people"></property>
</bean>
</beans>


测试类;测试Spring装配的结果

package com.zhiwei.autowire;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Spring装配:创建IOC容器中bean的写作关系的行为称为装配wiring
 * bean注入的方式:
 *   1: property:直接属性注入(set)
 *   2:constructor-arg:构造函数注入:index 表示参数的序列号
 * 注意:使用ref关联外部bean的javabean不能包含构造方法,否则报错,ref关联属性只能通过set进行属性注入
 * 构造方法顺序:默认构造方法注入---->set属性注入
 * bean初始化顺序:实例化bean按照applicationContext.xml配置顺序实例化
 *
 * 装配类型:
 *    @resource:byName-->byType,匹配不上报错
 *    @autowired:byType:匹配不上报错
 *    
 *    byName:IOC容器中的bean初始化时未找到初始化对象则根据属性名和IOC容器中id相同的bean关联,注意最后是通过构造函数  注入对象
 *    byType:Bean初始化时未指定默认的属性值,则更具属性类型一致的其他bean关联
 *    constructor:将关联的对象作为构造函数的参数装配进当前bean
 *    autodetect:自动检测当前的byType和constuctor装配方式,不行则报错
 *    default:beans设置的默认装配方式:default-autowire="autodetect"
 *    no:无默认配置方式
 *
 * IOC容器:ApplicationContext和SessionFactory
 */
public class MainTest {

    public static void main(String[] args) {
        
        //初始化IOC容器
        ApplicationContext ac=new ClassPathXmlApplicationContext("com/zhiwei/autowire/applicationContext.xml");
        
        House house=(House) ac.getBean("house");
        System.out.println("house:"+house);
        
        People people=(People) ac.getBean("people");
        System.out.println("people:"+people);
    }
}

0 0