Spring框架
Spring 容器创建对象的方式
接上简述一 :
https://blog.csdn.net/kaema/article/details/121622097
(注:案例中的代码都是沿用简述一的代码,若有兴趣可以点击上面的链接查看)
一已经说过使用默认的构造方法创建对象,接下来就是使用其他方法创建对象
在实体类 Student 中添加带参数的构造方法:
public Student(Integer id, String name, String classRoom) {
this.id = id;
this.name = name;
this.classRoom = classRoom;
}
创建工厂类:
package com.imok.pojo;
public class Factory {
/**
* 实例方法
*/
public Student instanceFun(){
System.out.println("Factory--------instanceFun");
return new Student(1,"小白","三班");
}
/**
* 静态方法
*/
public static Student staticFun(){
System.out.println("Factory---------staticFun");
return new Student(2,"小黑","四班");
}
}
由于怕麻烦就放在pojo包下了…
创建新的配置文件 createType.xml:
通过默认构造方法创建
<!--1、通过默认构造方法-->
<bean id="student1" class="com.imok.pojo.Student"></bean>
通过带参数的构造方法创建
<!--2、通过带参数的构造方法-->
<bean id="student2" class="com.imok.pojo.Student">
<!--name:表示参数的名称-->
<constructor-arg name="id" value="7"/>
<constructor-arg name="name" value="圣枪哥"/>
<constructor-arg name=