spring 梳理3--依赖注入DI 构造器注入、set方法注入
2021-03-05 20:27
                         标签:www   lap   null   add   rgba   cond   构造器   包括   getter    控制反转(IOC)也叫依赖注入(DI)的核心思想是,构建对象(包括初始化和赋值)都不需要人为操作,而是将这个权利交付给容器来进行。   1、构造器注入   (1)编写javaBean     (2)无参构造     (3)有参构造 注意:value属性注入的是基本类型,如果是引用类型要使用ref连接其他的bean。           2、set方法注入 要求被注入的属性 , 必须有set方法 。  set方法的方法名由set + 属性首字母大写  如果属性是boolean类型 , 没有set方法 , 是 is + 属性首字母大写 .     (1)Address.java   (2)User.java     (3)配置文件,看各种类型的参数如何注入       3. 测试       spring 梳理3--依赖注入DI     构造器注入、set方法注入 标签:www   lap   null   add   rgba   cond   构造器   包括   getter    原文地址:https://www.cnblogs.com/Master-Sun/p/14316741.htmlpublic class Dog {
  private String name;
  private int age;
  public Dog(){}
  public Dog(String name, int age) {
    this.name = name;
    this.age = age;
  }
  public String getName() { return name;}
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() { return age; }
  public void setAge(int age) {
    this.age = age;
  }
}
bean id="goldilocks" class="com.xinzhi.entity.Dog" />
bean id="goldilocks" class="com.xinzhi.entity.Dog">
  constructor-arg name="name" value="goldilocks"/>
  constructor-arg name="age" value="11"/>
bean>
bean id="goldilocks" class="com.xinzhi.entity.Dog">
  constructor-arg index="0" value="goldilocks"/>
  constructor-arg index="1" value="11"/>
bean>
bean id="goldilocks" class="com.xinzhi.entity.Dog">
  constructor-arg value="goldilocks"/>
  constructor-arg value="11"/>
bean>
bean id="goldilocks" class="com.xinzhi.entity.Dog">
  constructor-arg type="java.lang.String" value="goldilocks"/>
  constructor-arg type="java.lang.Integer" value="11"/>
bean>
public class Address {
  private String addressInfo;
  public String getAddressInfo() {
    return addressInfo;
  }
  public void setAddressInfo(String addressInfo) {
    this.addressInfo = addressInfo;
  }
}
public class User {
  private String name;
  private Address address;
  //爱好
  private String[] hobbies;
  //职务
  private List
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">
  bean id="address" class="com.xinzhi.entity.Address" >
    property name="addressInfo" value="珠琳旺角大厦"/>
  bean>
  bean id="user" class="com.xinzhi.entity.User">
    
    property name="name" value="楠哥" />
    property name="address" ref="address"/>
    
    property name="hobbies">
      array>
        value>写程序value>
        value>吃value>
        value>喝value>
        value>漂亮姑娘value>
        value>赢钱value>
      array>
    property>
    
    property name="duties">
      list>
        value>IT老师value>
        value>我儿子的爸爸value>
      list>
    property>
    
    property name="carts">
      set>
        value>纸尿裤value>
        value>玩具value>
      set>
    property>
    
    property name="familyTies">
      map>
        entry key="father" value="张某某" />
        entry key="mather" value="钟某某" />
      map>
    property>
    
    property name="workExperience">
      props>
        prop key="first">电厂职工prop>
        prop key="second">java开发工程师prop>
        prop key="third">java讲师prop>
      props>
    property>
    
    property name="daughter">null />property>
  bean>
beans>
@Test
public void testDI(){
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  User user = applicationContext.getBean(User.class);
  System.out.println(user);
}

文章标题:spring 梳理3--依赖注入DI 构造器注入、set方法注入
文章链接:http://soscw.com/index.php/essay/60598.html