zoukankan      html  css  js  c++  java
  • 17_8_9 Spring 注入

    1 Spring 的 Bean 的属性注入:

    构造方法的方式注入属性:

    <!-- 第一种:构造方法的方式 -->
    <bean id="car" class="cn.itcast.spring.demo4.Car">
        <constructor-arg name="name" value="保时捷"/>
        <constructor-arg name="price" value="1000000"/> 
    </bean>
    

    set 方法的方式注入属性:

    <!-- 第二种:set 方法的方式 -->
    <bean id="car2" class="cn.itcast.spring.demo4.Car2">
        <property name="name" value="奇瑞 QQ"/>
        <property name="price" value="40000"/> 
    </bean>
    

    2 Spring 的属性注入:对象类型的注入:

    <bean id="person" class="cn.itcast.spring.demo4.Person">
        <property name="name" value="会希"/>
        <property name="car2" ref="car2"/>
    </bean>
    

    3 注入复杂类型:

    
    <bean id="collectionBean" class="cn.itcast.spring.demo5.CollectionBean">
    
    <!-- 数组类型的属性 --> //可以省去<List></List>
    
    <property name="arrs">
        <list> 
            <value>会希</value> 
            <value>冠希</value> 
            <value>天一</value>
        </list>
    </property>
    
    
    <!-- 注入 List 集合的数据 --> 
    
    <property name="list">
        <list> 
            <value>芙蓉</value> 
            <value>如花</value>
             <value>凤姐</value>
        </list>
    </property>
    
    
    <!-- 注入 Map 集合 --> 
    
    <property name="map">
        <map>
            <entry key="aaa" value="111"/> 
            <entry key="bbb" value="222"/> 
            <entry key="ccc" value="333"/>
        </map>
    </property>
    
    
    <!-- Properties 的注入 --> 
    
    <property name="properties">
        <props>
            <prop key="username">root</prop>
             <prop key="password">123</prop>
         </props>
    </property>
    
    </bean>
    

    4.工厂方法创建bean:

    5 引入外部属性文件

    一种方式:
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
        <property name="location" value="classpath:jdbc.properties"/>
    </bean>
    
    二种方式:
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
    
    

    Spring注入资料参考

  • 相关阅读:
    Using AlloyTouch to control three.js 3D model
    AlloyTouch与three.js 3D模型交互
    移动Web触摸与运动解决方案AlloyTouch开源啦
    transformjs玩转星球
    swing with transformjs
    和transformjs一起摇摆
    getting started with transformjs
    移动Web利器transformjs入门
    腾讯AlloyTeam移动Web裁剪组件AlloyCrop正式开源
    Why AlloyFinger is so much smaller than hammerjs?
  • 原文地址:https://www.cnblogs.com/du1991/p/7326055.html
Copyright © 2011-2022 走看看