美文网首页JAVA后台开发_从入门到精通
26 Spring由名称(Name)自动装配

26 Spring由名称(Name)自动装配

作者: 笑Skr人啊 | 来源:发表于2017-08-22 16:19 被阅读10次

    在Spring中,“按名称自动装配”是指,如果一个bean的名称与其他bean属性的名称是一样的,那么将自动装配它。
    例如,如果“customer” bean公开一个“address”属性,Spring会找到“address” bean在当前容器中,并自动装配。如果没有匹配找到,那么什么也不做。
    你可以通过 autowire="byName" 自动装配像下面这样:

    <!-- customer has a property name "address" -->
    <bean id="customer" class="com.gp6.autoAssemblyBean.Customer" autowire="byName" />
    
    <bean id="address" class="com.gp6.autoAssemblyBean.Address" >
        <property name="fulladdress" value="YiLong Road, CA 188" /> 
    </bean>
    
    

    看看Spring自动装配的完整例子。

    1. Beans

    这里有两个 beans, 分别是:customer 和 address.

    package com.gp6.autoAssemblyBean.name;
    
    public class Address {
        private String fulladdress;
    
        public String getFulladdress() {
            return fulladdress;
        }
    
        public void setFulladdress(String fulladdress) {
            this.fulladdress = fulladdress;
        }
    }
    
    
    
    package com.gp6.autoAssemblyBean.name;
    
    public class Customer {
        private Address address;
    
        public Address getAddress() {
            return address;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    }
    
    
    

    2. Spring 装配

    通常情况下,您明确装配Bean,这样通过 ref 属性:

    <!-- 通过ref手动装配-->
    <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-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <bean id="customer" class="com.gp6.autoAssemblyBean.name.Customer" >
            <property name="address" ref="address" />
        </bean>
        
        <bean id="address" class="com.gp6.autoAssemblyBean.name.Address" >
            <property name="fulladdress" value="YiLong Road, CA 188" /> </bean>
        
    </beans>
    
    

    输出

    YiLong Road, CA 188
    
    

    使用按名称启用自动装配,你不必再声明属性标记。只要在“address” bean是相同于“customer” bean 的“address”属性名称,Spring会自动装配它。

    
    <!-- 通过类型Name装配 -->
     <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-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <bean id="customer" class="com.gp6.autoAssemblyBean.name.Customer" autowire="byName" />
        
        <bean id="address" class="com.gp6.autoAssemblyBean.name.Address" >
            <property name="fulladdress" value="YiLong Road, CA 188" /> 
        </bean>
        
    </beans>
    

    相关文章

      网友评论

        本文标题:26 Spring由名称(Name)自动装配

        本文链接:https://www.haomeiwen.com/subject/pmuldxtx.html