zoukankan      html  css  js  c++  java
  • Spring第二节 注入依赖

    set方法注入依赖

    <?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">
    <!--注入userdao-->
        <bean id="userDao" class="com.jc.dao.impl.UserDaoImpl" scope="singleton"> </bean>
    <!--    注入userservice-->
        <bean id="userService" class="com.jc.service.impl.UserServiceImpl" scope="singleton">
    <!--把userdao注入到userservice中-->
            <property name="userDao" ref="userDao"></property>
    <!--            name标签为userservice类的setUserDao函数的后去除set 首字母小写的值 ref为被注入的id-->
        </bean>
    </beans>

    构造方法注入依赖

    <?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">
    <!--注入userdao-->
        <bean id="userDao" class="com.jc.dao.impl.UserDaoImpl" scope="singleton"> </bean>
    <!--    注入userservice-->
        <bean id="userService" class="com.jc.service.impl.UserServiceImpl" scope="singleton">
    <!--把userdao注入到userservice中-->
            <constructor-arg name="userDao" ref="userDao"></constructor-arg>
    <!--        构造函数注入依赖 name为构成函数参数 ref为被注入的id-->
        </bean>
    </beans>

    依赖中注入普通参数

    <bean id="userDao" class="com.jc.dao.impl.UserDaoImpl" scope="singleton">
            <property name="username" value="jc"></property>
    <!--        注入普通参数 name为set函数后面的-->
        </bean>

    在userdao中注入list集合

    <!--  注入在userdao中特殊参数  -->
        <bean id="userDao" class="com.jc.dao.impl.UserDaoImpl" scope="singleton">
            <!--  注入list参数 此list是string普通类型 则为value 引用可以用ref  -->
            <property name="stringList">
    <!--            name为set方法的-->
            <list>
                <value>1</value>
                <value>a</value>
                <value></value>
            </list>
        </property>
    
        </bean>

    ref引用只能引用被注入的依赖

    注入map

     <bean>  
      </property>
            <property name="userMap">
                <!--            name为set方法的-->
                <map>
                    <entry key="user1" value-ref="user1"></entry>
                    <entry key="user2" value-ref="user2"></entry>
    <!--                ref引用当前页面的id 也就是被注入的依赖 key自定义 -->
                </map>
            </property>
        </bean>


    注入properties

         <property name="properties">
                <props>
                    <prop key="p1"> 1111</prop>
                    <prop key="p2"> 2111</prop>
                    <prop key="p3"> 3111</prop>
                </props>
            </property>
  • 相关阅读:
    Ubuntu 16 安装redis客户端
    crontab 参数详解
    PHP模拟登录发送闪存
    Nginx配置端口访问的网站
    Linux 增加对外开放的端口
    Linux 实用指令之查看端口开启情况
    无敌的极路由
    不同的域名可以指向同一个项目
    MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error
    Redis 创建多个端口
  • 原文地址:https://www.cnblogs.com/ziwang520/p/15588361.html
Copyright © 2011-2022 走看看