本文供以下文章参考使用:
Spring笔记https://blog.youkuaiyun.com/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'}