XML配置里的Bean自动装配

Spring IOC 容器可以自动装配Bean。需要做的仅是在<bean>的autowire属性里指定自动装配的模式

byType(根据类型自动装配):若IOC容器有多个与目标Bean类型一致的Bean.在这种情况下,Spring将无法判定哪个Bean最合适该属性,所以不能执行自动装配。

byName(根据名称自动装配):必须将目标Bean的名称和属性名设置的完全相同。

constructor(通过构造器自动装配):当Bean中存在多个构造器时,此种自动装配方式将会很复杂。不推荐使用

autowire.xml配置文件:是使用了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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        ">

    <bean id="car" class="autowire.Car">
        <property name="carName" value="baoma"></property>
        <property name="carPrice" value="10000.00"></property>
    </bean>
    <bean id="address" class="autowire.Address">
        <property name="city" value="beijing"></property>
        <property name="street" value="zhonghuajie"></property>
    </bean>
    <!--使用autowire自动装配,而与它伴随的是p命名-->
    <bean id="person" class="autowire.Person" p:name="chen" p:age="23" autowire="byName"></bean>
</beans>
package autowire;

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

public class Main {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("spring1.xml");
        collection.Person person = (collection.Person) applicationContext.getBean ("person");
        System.out.println (person);
    }
}

Address类的文件

package autowire;

public class Address {
    public String city;
    public String street;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    @Override
    public String toString() {
        return "Address{" +
                "city='" + city + '\'' +
                ", street='" + street + '\'' +
                '}';
    }
}

 

Car类的文件

package autowire;

public class Car {
    public String carName;
    public double carPrice;
    public String getCarName() {
        return carName;
    }

    public void setCarName(String carName) {
        this.carName = carName;
    }

    public double getCarPrice() {
        return carPrice;
    }

    public void setCarPrice(double carPrice) {
        this.carPrice = carPrice;
    }

    @Override
    public String toString() {
        return "Car{" +
                "carName='" + carName + '\'' +
                ", carPrice=" + carPrice +
                '}';
    }
}

 

Main类文件:

package autowire;

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

public class Main {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("spring1.xml");
        collection.Person person = (collection.Person) applicationContext.getBean ("person");
        System.out.println (person);
    }
}

 

输出结果:

collection.Person{name='chen', age=56, cars=[Car{name='baoma', num=12, color='red', price=0.0}, Car{name='bentian', num=13, color='blue', price=0.0}]}

 

byType(根据类型自动装配):

autowire.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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        ">

    <bean id="car" class="autowire.Car">
        <property name="carName" value="baoma"></property>
        <property name="carPrice" value="10000.00"></property>
    </bean>
    <bean id="car1" class="autowire.Car">
        <property name="carName" value="bentian"></property>
        <property name="carPrice" value="2000000.00"></property>
    </bean>
    <bean id="address" class="autowire.Address">
        <property name="city" value="beijing"></property>
        <property name="street" value="zhonghuajie"></property>
    </bean>
    <!--使用autowire自动装配,而与它伴随的是p命名-->
    <!--<bean id="person" class="autowire.Person" p:name="chen" p:age="23" autowire="byName"></bean>-->
    <bean id="person" class="autowire.Person"  p:name="chen" p:age="32" autowire="byType"></bean>
</beans>

 

其它文件都是与上面一样的。

总结:

可以使用autowire属性指定自动装配的方式

byName 根据bean的名字和当前bean的setter风格的属性名进行自动装配,若有匹配,则进行自动匹配。若没有匹配的,则不装配。

byType 根据bean的类型和当前bean的属性的类型进行自动装配,若IOC容器中有1个以上的类型匹配的bean,则抛异常。会出现装配的bean不唯一的错误。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值