当前位置: 首页 > news >正文

Spring的依赖注入DI

调用构造器方法注入: 

        需要有对应匹配的构造器   不然出现异常No matching constructor

   Spring构造器注入有三种不同方式

name :按照参数名称

<bean id="stu" class="com.xja.bean.Student">

           <constructor-arg name="name" value="rk">  </constructor-arg>

</bean>      

  index:按照参数的位置 从0开始

     

<bean id="stu" class="com.xja.bean.Student">

           <constructor-arg index="0" value="rk">  </constructor-arg>

</bean>   

 type:按照参数的类型  类型有多个就设置多个

<bean id="stu" class="com.xja.bean.Student">

           <constructor-arg type="java.lang.String" value="rk">  </constructor-arg>

</bean>   

调用Set方法注入:

首先DI是在IOC的基础上完成的,使用注入首先在Spring配置文件创建一个Student对象

<bean id="stu" class="com.xja.bean.Student"></bean>

以下property标签皆在bean标签内部

@Date
public class Student{public String name ;public String[] array ;public Grade grade;public List list;public Set set;public Map map;public Properties prop;//使用IOC默认使用无参构造器创建对象,没有无参构造器会报错      //   public Student(String stuName){//       this.stuName = stuName;//   }public Student(){System.out.println("student被创建。。。")}public void init(){System.out.println("init被创建。。。")}public void destory(){System.out.println("destory被创建。。。")}}@Dateclass Grade{public Integer gid;public String gname;
}

   注入一个String类型的name属性   

  <property name="name" value="admin"></property>

   注入一个数组

    <property name = "array">

                <array>

                        <value>11</value>

                        <value>22</value>

                        <ref>grade</ref>

                </array>

        </property>

 注入一个Grade对象 依赖注入在IOC的基础上  容器中要有Grade对象

<property name="grade" ref="grade"></property>

<bean id="grade" class="com.xja.bean.Grade">

注入一个Map集合

<property name="map">

        <map>

                <entry key="a" value="1">

                <entry key="b" value-ref="grade">

        </map>

</property>

<property name="prop">

      <props>

                <prop key="uname">admin</prop>

                <prop key="pwd">123</prop>

        </props>

</property>

注入一个set集合

<property name = "set">

                <set>

                        <value>11</value>

                        <value>22</value>

                        <value>11</value>   //set特性不可重复,后添加的无效

                </set>

 </property>

http://www.lryc.cn/news/359972.html

相关文章:

  • Java 获取和修改期日与时间的各种操作方法
  • 【ubuntu20】--- 定时同步文件
  • 网吧|基于SprinBoot+vue的网吧管理系统(源码+数据库+文档)
  • [C/C++] -- Libcurl开发
  • Streamsets-JDBC模式使用更新时间字段数据同步
  • Nodejs-- 网络编程
  • React@16.x(14)context 举例 - Form 表单
  • 十几款基于ChatGPT的免费神器,每个都是王炸!
  • devicemotion 或者 deviceorientation在window.addEventListener 事件中不生效,没有输出内容
  • java单元测试如何断言异常
  • C语言| n的阶乘相加
  • cwiseMax、cwiseMin函数
  • 【thinkphp问题栏】tp5.1重写URL,取消路径上的index.php
  • 缓冲字符流
  • Django中使用Celery和APScheduler实现定时任务
  • Kivy.uix.textinput
  • 基于IoTDB 平台的学习和研究
  • nessus plugins目录为空的问题
  • FDW(Foreign Data Wrapper)
  • Flutter开发指南
  • SpringCloud学习笔记万字整理(无广版在博客)
  • c++(七)
  • SQL语言
  • 【PPT】修改新建文本框默认字体
  • 智能办公本如何选择
  • Spark基础:Scala变量与数据类型
  • php 实现:给图片加文字水印,图片水印,压缩图片
  • 免费实现网站HTTPS访问
  • vue3使用vue3-print-nb打印
  • R语言ggplot2包绘制网络地图