Spring中Di(依赖注入)之Set注入

目录

1、引用类型手工注入(ref方式)

2、引用类型自动注入

2.1 byName(按名称注入)

2.2 byType

2.2.1 第一种同源(一致性)

2.2.2第二种同源(父子类)

2.2.3第三种同源(接口和实现类)


1、引用类型手工注入(ref方式)

<?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.xsd">
<!-- 手工注入   手动设置ref中的属性值与User类的属性值匹配-->
    <bean class="pojo.User" id="user">
        <property name="id" value="1"/>
        <property name="name" value="毛毛"/>
        <property name="role" ref="role"/>
    </bean>
<!--    这里的bean的id要与上面的ref的值一致,同样为User类的属性值-->
    <bean class="pojo.Role" id="role">
        <property name="RId" value="1"/>
        <property name="RName" value="护士"/>
    </bean>
</beans>

2、引用类型自动注入

2.1 byName(按名称注入)

java类中引用类型的属性名和spring容器中(配置文件)的id名称一样且 数据类型是一致的,这样的容器中的bean,spring能够赋值给引用类型

<?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.xsd">
    <!--    自动注入 autowire是自动的意思,里面的值会自动与class类的属性值匹配,进而识别下面的bean-->
    <bean class="pojo.User" id="user" autowire="byName">
        <property name="id" value="1"/>
        <property name="name" value="毛毛"/>
    </bean>
    <bean class="pojo.Role" id="role">
        <property name="RId" value="1"/>
        <property name="RName" value="老师"/>
    </bean>
</beans>      

2.2 byType

byType(按类型注入):java类中引用类型的数据类型和spring容器中(配置文件)的class属性

是同源关系的,这样的bean能够赋值给引用类型 注: 同源即同一类型的意思:

        2.2.1 第一种同源(一致性)

        java引用类型的数据类型和bean的class的值是一样的

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

<!--    自动注入之byType的第一种同源 (一致性):java引用类型的数据类型和bean的class的值是一样的-->
    <bean class="pojo.User" id="user" autowire="byType">
        <property name="id" value="1"/>
        <property name="name" value="毛毛"/>
    </bean>
<!--    这里的id是用不到的,通常为了规范我们还是会把它命名为对应数据类型的对象加上去-->
    <bean class="pojo.Role" id="role">
        <property name="RId" value="1"/>
        <property name="RName" value="老师"/>
    </bean>
</beans>

        2.2.2第二种同源(父子类)

        java引用类型的数据类型和bean的class的值是父子类间的关系

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

    <!--    自动注入之byType的第二种同源(父子类):java引用类型的数据类型和bean的class的值是父子类间的关系-->
    <bean class="pojo.User" id="user" autowire="byType">
        <property name="id" value="1"/>
    </bean>
    <bean class="pojo.SonRole" id="sonRole">
        <property name="name" value="张三"/>
    </bean>

<!--        多个子类-->
<!--    <bean class="pojo.User" id="user" autowire="byType">-->
<!--        <property name="id" value="1"/>-->
<!--    </bean>-->
<!--    <bean class="pojo.SonRole" id="sonRole">-->
<!--        <property name="name" value="张三"/>-->
<!--    </bean>-->
<!--    <bean class="pojo.SonRole1" id="sonRole1">-->
<!--        <property name="password" value="123"/>-->
<!--    </bean>-->

<!--        调用子类的方法-->
<!--    <bean class="pojo.User" id="user" autowire="byType">-->
<!--        <property name="id" value="1"/>-->
<!--    </bean>-->
<!--    <bean class="pojo.SonRole" id="sonRole">-->
<!--        <property name="name" value="张三"/>-->
<!--        <property name="sex" value="男"/>-->
<!--    </bean>-->

</beans>

        2.2.3第三种同源(接口和实现类)

        java引用类型的数据类型和bean的class的值是接口和实现类的关系

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

    <!--    自动注入之byType的第三种同源 (接口和实现类):java引用类型的数据类型和bean的class的值是接口和实现类的关系-->
    <bean class="service.UserServiceImpl" id="userService" autowire="byType"/>
    <!--    这里的id是用不到的,通常为了规范我们还是会把它命名为对应数据类型的对象加上去-->
    <bean class="mapper.UserMapperImpl" id="userMapper"/>
</beans>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值