spring依赖注入

1、  依赖注入—dependency injection

bean对象依赖于spring容器创建,bean对象依赖的资源由容器来设置和装配

  • l  谁依赖于谁:应用程序依赖于IoC容器;
  • l  为什么需要依赖:应用程序需要IoC容器来提供对象需要的外部资源;
  • l  谁注入谁: IoC容器注入应用程序某个对象,应用程序依赖的对象;
  • l  注入了什么:注入某个对象所需要的外部资源(包括对象、资源、常量数据)。

2、  spring注入方式—--构造器注入

见spring ioc创建对象

3、  spring注入方式----setter注入

要求被注入的属性必须有set方法。Set方法的方法名由set+属性首字母大写。如果属性是boolean没有get方法,是is

a)         常量注入

<bean name="student" class="com.silvan.pojo.Student">
    <property name="name" value="zhangsan"></property>
</bean>
//student类
public class Student {
    private String name;
    public void setName(String name) {
       this.name = name;
    }
    public void show(){
       System.out.println("name="+name+"  addr="+addr.getAddress());
    }
}
//测试
public static void main(String[] args) {
    ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
    Student stu = (Student) ac.getBean("student");
    stu.show();
}


b)         Bean注入

//bean配置文件
<bean name="addr" class="com.silvan.pojo.Address">
    <property name="address" value="shandong"></property>
</bean>
<bean name="student" class="com.silvan.pojo.Student">
    <property name="name" value="zhangsan"></property>
    <property name="addr" ref="addr"></property>
</bean>
//student类
public class Student {
    private String name;
    private Address addr;
    public void setAddr(Address addr) {
       this.addr = addr;
    }
    public void setName(String name) {
       this.name = name;
    }
    public void show(){
       System.out.println("name="+name+"  addr="+addr.getAddress());
    }
}
//address类
public class Address {
    private String address;
    public String getAddress() {
       return address;
    }
    public void setAddress(String address) {
       this.address = address;
    }
}
//测试
public static void main(String[] args) {
    ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
    Student stu = (Student) ac.getBean("student");
    stu.show();
}

c)         数组注入

<bean name="student" class="com.silvan.pojo.Student">
      <property name="name" value="zhangsan"></property>
      <property name="addr" ref="addr"></property>
      <property name="books">
         <array>
            <value>山居笔记</value>
            <value>撒哈拉的故事</value>
            <value>基督山伯爵</value>
            <value></value>
         </array>
      </property>
   </bean>

 d)         List注入

<property name="hobbies">
         <list>
            <value>看书</value>
            <value>听歌</value>
            <value>看电视</value>
            <value>运动</value>
         </list>
      </property>

e)         Map注入

<property name="cards">
    <map>
       <entry key="bank of china" value="1324325325"></entry>
       <entry>
           <key><value>工商银行</value></key>
           <value>75784758748784</value>
       </entry>
    </map>
</property>


f)          Set注入

<property name="games">
         <set>
            <value>三国</value>
            <value>lol</value>
            <value>王者荣耀</value>
         </set>
      </property>


g)         Null注入

<property name="wife"><null/></property>

h)         Properties注入 

<property name="info">
         <props>
            <prop key="学号">21203</prop>
            <prop key="sex">boy</prop>
            <prop key="name">小强</prop>
         </props>
      </property>

i)           P命名空间注入

//配置文件头部加入p声明
<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.xsd">
//p命名空间注入
<!-- p命名空间注入 属性依然要设置set方法 -->
    <bean name="user" class="com.silvan.pojo.User" p:name="杨幂" p:age="29"></bean>

j)           C命名空间注入

//头部配置
<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"
     xmlns:c="http://www.springframework.org/schema/c" 
xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- c命名空间注入要求有对应参数的构造方法 --> <bean name="user1" class="com.silvan.pojo.User" c:name="赵丽颖" c:age="26"></bean>

  

原文地址:https://www.cnblogs.com/zhouyeqin/p/7207603.html