Bean的依赖注入

 目录结构

 

1、构造函数的注入

   <!--     1、 构造函数的注入,构造函数的参数个数必须与constructor-arg个数一致-->
<bean id="count" class="com.wjm.model.Count" scope="prototype">
         <constructor-arg name="name" value="张三"/>
         <constructor-arg name="age" value="24"/>
         <constructor-arg name="date" ref="now"/>
     </bean>
    <bean id="now" class="java.util.Date"/>
public class Count {
  private String name;
  private Integer age;
//  private String[] starr;
//  private List<String> myList;
//  private Map<String,String> myMap;
//  private Properties myProps;
  Date date;

  public Count() {

  }

  public Count(String name, Integer age, Date date) {
    this.name = name;
    this.age = age;
    this.date=date;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setAge(Integer age) {
    this.age = age;
  }

  public void setDate(Date date) {
    this.date = date;
  }

  @Override
  public String toString() {
    return "Count{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", date=" + date +
            '}';
  }

2、set方法注入

<!--    2、set方法注入,必须要有默认的构造方法和实体类中的set方法-->
   <bean id="count2" class="com.wjm.model.Count">
       <property name="name" value="李四"/>
       <property name="age" value="20"/>
       <property name="date" ref="now"/>
   </bean>

3、集合数据的注入需要set方法注入

<!--    集合数据依靠set方式注入-->
    <bean id="countList" class="com.wjm.model.CountList">
        <property name="name" value="王五"/>
        <property name="age" value="22"/>
        <property name="myList">
            <list>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </list>
        </property>
        <property name="starr">
            <array>
                <value>字符串1</value>
                <value>字符串2</value>
                <value>字符串3</value>
            </array>
        </property>
        <property name="date" ref="now"/>
        <property name="myMap">
            <map>
                <entry key="键1" value="值1"/>
                <entry key="键2" value="值2"/>
                <entry key="键3" value="值3"/>
            </map>
        </property>
        <property name="myProps">
            <props>
                <prop key="prop1">prop值1</prop>
                <prop key="prop2">prop值2</prop>
                <prop key="prop3">prop值3</prop>
            </props>
        </property>
    </bean>
public class CountList {
    private String name;
    private Integer age;
    private String[] starr;
    private List<String> myList;
    private Map<String,String> myMap;
    private Properties myProps;
    private Date date;

    public CountList() {
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setStarr(String[] starr) {
        this.starr = starr;
    }

    public void setMyList(List<String> myList) {
        this.myList = myList;
    }

    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }

    public void setMyProps(Properties myProps) {
        this.myProps = myProps;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "CountList{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", starr=" + Arrays.toString(starr) +
                ", myList=" + myList +
                ", myMap=" + myMap +
                ", myProps=" + myProps +
                ", date=" + date +
                '}';
    }
public class Control {
   public static void main(String[] args) {

      ApplicationContext applicationContext=new ClassPathXmlApplicationContext("BeanConfig/Bean.xml");//可在Bean.xml中设置为懒加载
      Count count=applicationContext.getBean("count2",Count.class);
      System.out.println(count.toString());

      CountList countList=applicationContext.getBean("countList",CountList.class);
      System.out.println(countList.toString());


   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值