• 依赖注入


    一.通过属性的setter方法注入

    两种依赖注入的区别:

    在beans.xml文件中的配置,

    1.

    <property name="ooo"></property>--------------->用于为属性注入值

    <property ref="xxx"></property>------------------->根据xxx在spring容器中得到bean,根据反射技术将bean附给了ooo属性

    可以给很多bean注入值 

     2.

    使用内部bean实现依赖注入

    <bean>

    <property name="personDao">

      <bean class=""></bean>

    </property>

    </bean>

    只能给这个bean注入值 

    怎么给基本类型的属性注入值 ?

    <bean>

    <property name="name" value="xxx"/>--------------------->将xxx注入给name属性

    </bean>

    怎么给set集合类型注入值?

    <bean .......>

    <property name="sets">

    <set>

      <value>first</value>

      <value>second</value>

      <value>third</value>

    </set>

    </property>

    </bean>

    怎么给list集合类型注入值?

    <bean .......>

    <property name="lists">

    <list>

      <value>first</value>

      <value>second</value>

      <value>third</value>

    </set>

    </property>

    </bean>

    怎么给Properties集合类型注入值?

    <bean .......>

    <property name="properties">

    <props>

      <prop key="key1">value1</prop>

      <prop key="key2">value2</prop>

    </props>

    </property>

    </bean>

    怎么给HashMap集合类型注入值?

    <bean .......>

    <property name="maps">

    <map>

      <entry key="key1" value="value1"></entry>

      <entry key="key2"  value="value2"></entry>

    </map>

    </property>

    </bean>

    二.通过构造器来注入

    有两个属性:PersonDao 和name两个属性

    <bean ...>

    <constructor-arg index="0" type="com.cn.dao.PersonDao" ref="personDao"/>

    <constructor-arg index="1" value="spring is coming"/>

    </bean>

    三.使用Field注入(用于注解方式)

    加入两个命名空间:

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

    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd

     <context:annotation-config/>------------>将注解写到Spring容器

    导入jar文件

    lib\j2ee\common-annotations.jar

    在java代码中使用@Autowired或者@Resource注解方式进行装配;两者的区别:@Autowired 默认按类型装配 @Resource默认按照名称装配,找不到与名称匹配的bean才会按照类型装配。

  • 相关阅读:
    How to join (ap invoice distributions all) AP table info with PO Table
    Order Management APIs Oe_Order_Pub
    R12 Oe_Order_Pub API
    物料搬运单
    Process Order API In Order Management(详细)
    ap_invoice_distributions_all与PO表关联问题
    分配用人单位
    oe_order_pub 更改订单行数量,提示此更改的原因未提供或无效
    Order Management APIs
    提前期
  • 原文地址:https://www.cnblogs.com/GodFather001/p/2288338.html
Copyright © 2020-2023  润新知