• Spring 依赖注入


    1.构造注入

    1.创建实体类User

    2.创建dao层接口及两个实现类user1daoimpl,user2daoimpl

    3.创建业务层接口及实现类userbizImpl

     

    1. 为实现类编写构造注入。

     

    1. 编写文件头

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

    1. 在配置文件中调用。

     

    6.测试构造注入。

    p命名空间注入

    1.加入文件头

    <?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.0.xsd">

    2.给实体注入属性。

    <bean id="user" class="com.bdqn.entity.User"

           p:id="2" p:name="随便一个名字" p:password="自定义密码"

          >

    </bean>

    3.为业务层注入组件

    <bean id="biz" class="com.bdqn.biz.impl.UserBizImpl" p:dao-ref="dao1">

            

    </bean>

    注意:注入组件要用p:dao-ref

    其他注入

    <?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="en" 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="name">
    <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>

    窗竹影摇书案上,野泉声入砚池中。 少年辛苦终身事,莫向光阴惰寸功
  • 相关阅读:
    CCPlatformConfig(设置执行平台 iOS android win32等。。。)
    LeetCode Add Binary |My Solution
    actor中!(tell)与forward的差别
    二叉查找树(二叉排序树)创建,插入,删除操作。
    程序猿技术练级攻略
    poj 2236 Wireless Network 【并查集】
    leetcode 题解 || Letter Combinations of a Phone Number 问题
    创业之前 ——Paul Graham 最新博文
    linux-文件系统基本概念
    上拉电阻和下拉电阻的用处和区别
  • 原文地址:https://www.cnblogs.com/qhantime/p/10745975.html
Copyright © 2020-2023  润新知