zoukankan      html  css  js  c++  java
  • 课时4:特殊值的注入问题和各种类型的自动装配

    .1)特殊值的注入问题

    <constructor-arg value="何邦柱" index="0"></constructor-arg>
    <constructor-arg  index="1"><value>20</value></constructor-arg>

        1.1                使用子元素<value>注入                而使用value属性注入

           参数的位置            写在首尾标签<value></value>中间(不加双引号)。   写在value的属性值中(必须加双引号)

           type属性             有(可选) 可以通过type属性指定数据类型。         无。

           参数值包含特殊字    两种处理方法:1.使用<![CDATA[ ]]标>记。        一种处理方法:即使用xml预定义的实体引用。

           符(<,&)时的处理方法。      2.使用xml预定义的实体引用。

        1.2 如何处理参数值包含特殊符号

          1.2.1 使用XML预定义的实体引用

      <bean id="Teacher" class="net.bdqn.hbz.pojo.Teacher">
    <!--        <property name="teaName" value="何邦柱"></property>-->
    <!--        <property name="teaAge" value="20"></property>-->
            <constructor-arg value="何邦柱&lt;&amp;" index="0"></constructor-arg>
            <constructor-arg  index="1">
                <value>20</value>
            </constructor-arg>
        </bean>

            1.2.1.1 &lt -----> <                                     &amp代表 --->&

          1.2.2 使用<![CDATA[ ]]标记

    <!--    课程类-->
        <bean id="Scourse" class="net.bdqn.hbz.pojo.Scourse">
            <property name="CName" >
                <value>java<![CDATA[ >>>> ]]></value>
            </property>
            <property name="CHour" value="200"></property>
            <property name="teaCher" ref="Teacher"></property>
    <!--        <constructor-arg value="java" index="0" name="cName" type="java.lang.String"></constructor-arg>-->
    <!--        <constructor-arg value="200" index="1"></constructor-arg>-->
    <!--        <constructor-arg ref="Teacher" index="2"></constructor-arg>-->
        </bean>

      2.constructor注入的特殊值处理与set注入是一样的

      3.如何处理null值以及""值

        3.1 给元素赋值null

    <property name="CName" >
                <null/>
            </property>

        3.2 给元素赋值“”

        <property name="CName" >
                <value></value>
            </property>
    或者
            <property name="CName" value="">
            </property>

    .2)在定义ioc的前提:该bean的类必须要有无参构造

    .3)自动装配(只适用于 ref类型):

      1.约定大于配置

      2.如何使用byName自动装配 byName的本质就是byId

       <bean id="Scourse" class="net.bdqn.hbz.pojo.Scourse" autowire="byName">
      </bean>

        2.1 自动寻找:其他bean的值id值=该类的属性名称

      3.如何使用byType自动装配

        <bean id="Scourse" class="net.bdqn.hbz.pojo.Scourse" autowire="byType">
    </bean>

        3.1 自动寻找bean的class类型=该类的属性类型(注意:此种方式 必须满足:当ioc容器中 只能有一个Bean满足条件 )

      4.如何使用constructor自动装配

        <bean id="Scourse" class="net.bdqn.hbz.pojo.Scourse" autowire="constructor">
    </bean>

        4.1 其他的bean的类型(class) 是否与 该Course类的构造方法参数的类型一致 ;本质上就是byType

      4.可以在头文件中一次性将该ioc容器的所有bean统一设置成自动装配

    <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"
           default-autowire="byName"
    >

        4.1 这虽然可以减少代码量 ,但是会降低可读性,使用时需要谨慎

    .4)使用注解自定义bean:通过注解方式 将bean以及相应的属性值 放入ioc容器

      1.首先声明头部

        xmlns:context="http://www.springframework.org/schema/context"
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd"

       2.开始配置扫描器 (如果有多个包 使用逗号隔开)

    <!--    配置扫描器 扫描注解形式的bean对象-->
        <context:component-scan base-package="net.bdqn.hbz.dao"/>

      3.声明注解类放入bean

    @Component("IStudentDao")
    public class IStudentDao {
        public Integer addStudent(Student student){
            System.out.println("你好......我是何邦柱");
            return 1;
        }

      4.@Component("IStudentDao)范围太大了 可以细化:

        4.1 dao层使用:@Repository("IStudentDao")

        4.2 service层使用:@Service("IStudentDao")

        4.3 控制层使用:@Controller("IStudentDao")

     

  • 相关阅读:
    Maximum Flow Exhaustion of Paths Algorithm
    ubuntu下安装java环境
    visualbox使用(二)
    vxworks一个超级奇怪的错误(parse error before `char')
    February 4th, 2018 Week 6th Sunday
    February 3rd, 2018 Week 5th Saturday
    February 2nd, 2018 Week 5th Friday
    February 1st, 2018 Week 5th Thursday
    January 31st, 2018 Week 05th Wednesday
    January 30th, 2018 Week 05th Tuesday
  • 原文地址:https://www.cnblogs.com/thisHBZ/p/12484542.html
Copyright © 2011-2022 走看看