Spring的依赖注入

    

@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被创建。。。")
    }

}

@Date
 class Grade{
     public Integer gid;
     public String gname;
}

使用Set:需要有对应的set方法

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

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

<property name = "set">

                <set>

                        <value>11</value>

                        <value>22</value>

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

                </set>

 </property>

使用构造器:  需要有对应匹配的构造器   No matching constructor

Spring依赖注入不同类型

        name :按照参数名称

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

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

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

相关推荐

  1. Spring依赖注入

    2024-05-25 18:14:38       10 阅读
  2. Spring DI 依赖注入

    2024-05-25 18:14:38       19 阅读
  3. Spring依赖注入方式

    2024-05-25 18:14:38       37 阅读
  4. Spring 依赖注入

    2024-05-25 18:14:38       13 阅读
  5. Spring注解实现依赖注入

    2024-05-25 18:14:38       36 阅读
  6. spring(二):基于注解实现依赖注入

    2024-05-25 18:14:38       29 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-25 18:14:38       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-25 18:14:38       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-25 18:14:38       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-25 18:14:38       18 阅读

热门阅读

  1. JVM-调优之-高内存占用问题排查

    2024-05-25 18:14:38       10 阅读
  2. OOM不会导致JVM退出

    2024-05-25 18:14:38       9 阅读
  3. 「Electron」Electron 应用程序详解

    2024-05-25 18:14:38       12 阅读
  4. 什么是UDP服务器?

    2024-05-25 18:14:38       8 阅读
  5. 根据标签名递归读取xml字符串中element

    2024-05-25 18:14:38       9 阅读
  6. 网络协议——有状态协议和无状态协议

    2024-05-25 18:14:38       10 阅读
  7. C#拼接xml

    2024-05-25 18:14:38       11 阅读
  8. xmlhttp中withcredential用法

    2024-05-25 18:14:38       11 阅读