zoukankan      html  css  js  c++  java
  • Spring IOC容器中注入bean

    一、基于schema格式的注入

    1、基本的注入方式 (属性注入方式)  

    根据setXxx()方法进行依赖注入,Spring只会检查是否有setter方法,是否有对应的属性不做要求

    <bean id="student" class="com.lq.ioc.Student">
            <property name="name" value="zhansan"></property>
            <property name="age" value="20"></property>
    </bean>
    <bean id="student" class="com.lq.ioc.Student">
            <property name="name">
                <value>zhangsan</value>
            </property>
            <property name="age">
                <value>20</value>
            </property>
    </bean>

    2.构造函数方式注入

    <bean id="student1" class="com.lq.ioc.Student">
            <constructor-arg name="name" value="zhangsan"></constructor-arg>
            <constructor-arg name="age" value="20"></constructor-arg>
    </bean>
    <bean id="student1" class="com.lq.ioc.Student">
            <constructor-arg type="java.lang.String"><value>lisi</value></constructor-arg>
            <constructor-arg type="int"><value>15</value></constructor-arg>
    </bean>
    <bean id="student1" class="com.lq.ioc.Student">
            <constructor-arg index="0" value="zhangsan"></constructor-arg>
            <constructor-arg index="1" value="21"></constructor-arg>
    </bean>

    3.当注入的属性中含有xml中的特殊字符时,如: < > & " '    

      1.用<![CDATA[<wangwu>]]>

    <bean id="student1" class="com.lq.ioc.Student">
            <constructor-arg index="0">
                <value><![CDATA[<wangwu>]]></value>
            </constructor-arg>
            <constructor-arg index="1" value="21"></constructor-arg>

    </bean>

      2.用转义字符

    <bean id="student1" class="com.lq.ioc.Student">
            <property name="name">
                <value>&lt;wang&gt;</value>
            </property>
            <property name="age" value="11"></property>
    </bean>

    4.引用其它bean 

    <bean id="school" class="com.lq.ioc.School"/>
        <bean id="student1" class="com.lq.ioc.Student">
            <property name="name">
                <value>&lt;wang&gt;</value>
            </property>
            <property name="age" value="11"></property>
            <property name="school" ref="school"></property>

    </bean>

    5.内部bean

    <bean id="student1" class="com.lq.ioc.Student">
            <property name="name">
                <value>&lt;wang&gt;</value>
            </property>
            <property name="age" value="11"></property>
            <property name="school">
                <bean id="school" class="com.lq.ioc.School"/>
            </property>
    </bean>

    6.往bean中注入null

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

    7.集合类属性

    List

    <bean id="school" class="com.lq.ioc.School">
            <property name="name" value="ql"/>
            <property name="student">
                <list>
                    <ref bean="student1"/>
                </list>
            </property>
    </bean>

    set

    <bean id="school" class="com.lq.ioc.School">
            <property name="name" value="ql"/>
            <property name="student">
                <set>
                    <ref bean="student1"/>
                </set>
            </property>
    </bean>

    map

    <bean id="school" class="com.lq.ioc.School">
            <property name="name" value="ql"/>
            <property name="student">
                <map>
                    <entry>
                        <key><ref bean="student1"/></key>
                        <value>1</value>
                    </entry>
                    
                </map>
            </property>
    </bean>

    props

    <bean id="school" class="com.lq.ioc.School">
            <property name="name" value="ql"/>
            <property name="student">
                <props>
                    <prop key="student1">zhansa</prop>
                    <prop key="student2">lisi</prop>
                </props>
            </property>
    </bean>

    8.通过util配置集合

    <util:properties id="List1">
            <prop key="student1">zzz</prop>
            <prop key="student2">lisi</prop>
    </util:properties>

    9.工厂类注入

    <bean id="studentFactory" class="com.lq.ioc.StudentFactory"/>
        <bean id="student" factory-bean="studentFactory" 
                             factory-method="createStudent"/>

      静态工厂类注入

    <bean id="student" class="com.lq.ioc.StudentFactory" 
                             factory-method="createStudent"/>

    10.配置数据源

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
               destroy-method="close"  
               p:driverClassName="com.mysql.jdbc.Driver"
               p:url="jdbc:mysql://localhost/sampledb"
               p:username="root"
               p:password="123"/>
  • 相关阅读:
    前端利用百度开发文档给的web服务接口实现对某个区域周边配套的检索
    libevent源码学习(13):事件主循环event_base_loop
    libevent源码学习(11):超时管理之min_heap
    libevent源码学习(10):min_heap数据结构解析
    libevent源码学习(8):event_signal_map解析
    libevent源码学习(9):事件event
    libevent源码学习(6):事件处理基础——event_base的创建
    libevent源码学习(5):TAILQ_QUEUE解析
    仿Neo4j里的知识图谱,利用d3+vue开发的一个网络拓扑图
    element表格内每一行删除提示el-popover的使用要点
  • 原文地址:https://www.cnblogs.com/coderising/p/5813665.html
Copyright © 2011-2022 走看看