首页 > 其他分享 >Spring Study -lesson07-03-15

Spring Study -lesson07-03-15

时间:2023-03-15 20:57:34浏览次数:33  
标签:byType 03 Spring Study byName lesson07 id

自动装配Bean  两种自动装配方法:第一byName  ,第二byType 属性类型(用byType可以省略写id)。

byName 要保证所有bean的id必须唯一 ,这个bean 必须和自动注入的属性set方法的值一致

<?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.5.xsd">

    <bean id="cat" class="com.feijian.pojo.Cat"/>
    <bean id="dog" class="com.feijian.pojo.Dog"/>
    <!--自动装配 byName 会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid!
                byType 会自动在容器上下文中查找,和自己对象属性类型相同的bean-->
    <bean id="people" class="com.feijian.pojo.People" autowire="byName">
        <property name="name" value="飞剑喜欢猫和狗"/>
<!--        <property name="cat" ref="cat"/>-->
<!--        <property name="dog" ref="dog"/>-->
    </bean>
</beans>

标签:byType,03,Spring,Study,byName,lesson07,id
From: https://www.cnblogs.com/RUI2022/p/17219966.html

相关文章