【Spring】- 属性注入方式

亡梦爱人 提交于 2019-11-29 05:19:38

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](https://my.oschina.net/u/1162528)

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;
    }
        
    @Overridepublic 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](https://my.oschina.net/u/1162528)
    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.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.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"/>
    </bean>
</beans>

测试类:

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的bEAN不能包含构造方法,否则报错,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:无默认配置方式 
*/

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);}}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!