Spring框架学习记录之构造注入

Spring框架学习记录之构造注入

CreateBy:雀雀

Date:2018.5.29

概念(图片来源于网络):


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"
>

    <bean
id="userDao" class="dao.impl.UserDaoImpl"></bean>
    <bean
id="userService" class="service.impl.UserServiceImpl">
       
<!--通过constructor-arg元素为构造方法里的参数赋值-->
       
<constructor-arg>
            <ref
bean="userDao"/>
        </constructor-arg>
    </bean>
</beans>

 

Spring框架P命名空间 = 给属性赋值的一种方式

        <?xml version="1.0" encoding="UTF-8"?>
<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-3.2.xsd"
>
<!-- 使用p命名空间注入属性值 -->
<bean id="user" class="entity.User" p:username="张三" p:age="23"
     
p:email="zhangsan@bdqn.com" />
<bean
id="userDao" class="dao.impl.UserDaoImpl" />
<bean
id="userService" class="service.impl.UserServiceImpl" p:dao-ref="userDao" />
</beans>

 

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


<bean
id="entity" class="entity.TestEntity">
   
<!-- 使用<![CDATA[]]>标记处理XML殊字符 -->
   
<property name="specialCharacter1">
        <value>
<![CDATA[P&G]]></value>
    </property>
   
<!-- XML特殊字符替换为实体引用 -->
   
<property name="specialCharacter2">
        <value>
P&amp;G</value>
    </property>
   
<!-- 定义内部Bean -->
   
<property name="innerBean">
        <bean
class="entity.User">
            <property
name="username">
                <value>
Mr. Inner</value>
            </property>
        </bean>
    </property>
   
<!-- 注入List类型 -->
   
<property name="list">
        <list>
           
<!-- 定义List中的元素 -->
           
<value>足球</value>
            <value>
篮球</value>
        </list>
    </property>
   
<!-- 注入数组类型 -->
   
<property name="array">
        <list>
           
<!-- 定义数组中的元素 -->
           
<value>足球</value>
            <value>
篮球</value>
        </list>
    </property>
   
<!-- 注入Set类型 -->
   
<property name="set">
        <set>
           
<!-- 定义Set或数组中的元素 -->
           
<value>足球</value>
            <value>
篮球</value>
        </set>
    </property>
   
<!-- 注入Map类型 -->
   
<property name="map">
        <map>
            
<!-- 定义Map中的键值对 -->
           
<entry>
                <key>
                    <value>
football</value>
                </key>
                <value>
足球</value>
            </entry>
            <entry>
                <key>
                    <value>
basketball</value>
                </key>
                <value>
篮球</value>
            </entry>
        </map>
    </property>
   
<!-- 注入Properties类型 -->
   
<property name="props">
        <props>
           
<!-- 定义Properties中的键值对 -->
           
<prop key="football">足球</prop>
            <prop
key="basketball">篮球</prop>
        </props>
    </property>
   
<!-- 注入空字符串值 -->
   
<property name="emptyValue">
        <value></value>
    </property>
   
<!-- 注入null -->
   
<property name="nullValue">
        <null/>
    </property>
</bean>
</beans>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值