Spring实例参考03-通过构造方法创建对象

本文供以下文章参考使用:

Spring笔记https://blog.csdn.net/qq_34378496/article/details/128095043java对象:

package com.evenif.bean;

public class Index {

    private int id;
    private String name;
    
    //无参构造方法
    public Index() {
        System.out.println("create Index class");
    }
    //两个参数的构造方法
    public Index(int id, String name) {
        this.id = id;
        this.name = name;
        System.out.println("create Index class with params");
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Index{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

beans.xml配置文件:

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 通过无参构造方法创建对象,属性通过setter注入(如果没有对应属性的
    setter方法会报错)-->
    <bean id="index01" class="com.evenif.bean.Index">
        <property name="name" value="ip01"></property>
    </bean>
    <!-- 通过构造方法public Index(int id, String name)创建对象,通过参数下
    标注入属性 -->
    <bean id="index02" class="com.evenif.bean.Index">
        <constructor-arg index="0" value="1"/>
        <constructor-arg index="1" value="ca02"/>
    </bean>
    <!-- 通过构造方法public Index(int id, String name)创建对象,通过参数下标注
    入属性,参数数量不一致会报错!
      Exception encountered during context initialization - cancelling refresh 
      attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: 
      Error creating bean with name 'index03' defined in class path resource 
      [beans.xml]: Unsatisfied dependency expressed through constructor parameter 1:
       Ambiguous argument values for parameter of type [java.lang.String] - did you 
       specify the correct bean references as arguments?
      Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: 
      Error creating bean with name 'index03' defined in class path resource [beans.xml]: 
      Unsatisfied dependency expressed through constructor parameter 1: Ambiguous argument 
      values for parameter of type [java.lang.String] - did you specify the correct bean 
      references as arguments?
	-->
    <!--
    <bean id="index03" class="com.evenif.bean.Index">
        <constructor-arg index="0" value="2"/>
    </bean>
    -->
    <!-- 通过构造方法public Index(int id, String name)创建对象,通过参数类型注入属性 -->
    <bean id="index04" class="com.evenif.bean.Index">
        <constructor-arg type="int" value="4"/>
        <constructor-arg type="java.lang.String" value="ca04"/>
    </bean>
    <!-- 参数类型和数量需要一直,否则报错!
    <bean id="index05" class="com.evenif.bean.Index">
        <constructor-arg type="int" value="4"/>
    </bean>
    -->
    <!-- 通过构造方法public Index(int id, String name)创建对象,通过参数名称注入属性
    名称一致!数量一致!类型一致!否则报错-->
    <bean id="index06" class="com.evenif.bean.Index">
        <constructor-arg name="name" value="ca06"/>
        <constructor-arg name="id" value="6"/>
    </bean>

</beans>

测试主函数

package com.evenif;

import com.evenif.bean.Index;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        //注意这个xml文件位置要放对
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Index index01 = (Index) context.getBean("index01");
        System.out.println(index01);
        Index index02 = (Index) context.getBean("index02");
        System.out.println(index02);
        Index index04 = (Index) context.getBean("index04");
        System.out.println(index04);
        Index index06 = (Index) context.getBean("index06");
        System.out.println(index06);
    }
}
//执行结果:注意,即使不获取index01或index02依然会调用两个构造方法,
//当执行 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 时对象就创建了。
//        create Index class
//        create Index class with params
//        create Index class with params
//        create Index class with params
//        Index{id=0, name='ip01'}
//        Index{id=1, name='ca02'}
//        Index{id=4, name='ca04'}
//        Index{id=6, name='ca06'}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_evenif

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值