Spring创建对象的三种方式

第一种:通过构造方法创建

1. 无参构造方法:默认情况
实现步骤:
首先:需要在类中定义无参构造方法
其次:在applicationContext.xml文件中设置,代码如下

//id表示获取到的对象标识
//class表示创建哪个类的对象
<bean id="peo" class="com.bean.People" />

最后:通过加载applicationContext.xml创建对象,代码如下:

//因为applicationContext.xml文件名和路径自定义,所以选择根目录方法。
//当配置文件加载时,类已创建
  ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
  //Object obj= ac.getBean("peo");
  //成功创建people对象
  People people= ac.getBean("peo", People.class);
//扩展
//getBeanDefinitionNames(),Spring容器中目前所有管理的所有对象。
  String[] names= ac.getBeanDefinitionNames();
  for (String string : names) {
     System.out.println(string);
  }

2. 有参构造方法:需要在applicationContext中明确配置
实现步骤:
首先:需要在类中定义有参构造方法。
其次:在applicationContext中设置调用哪个构造方法创建对象。代码如下:

 <bean id="peo" class="com.bean.People" >
 //ref引用另一个bean,value基本数据类型或String等, 
// index表示参数的索引, name参数名,type表示参数类型(区分开关键字和封装类 int和Integer)
  <constructor-arg index="0" value="12"></constructor-arg>
  <constructor-arg index="1" value="张三"></constructor-arg>
 </bean>

最后:测试代码如下

  ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
  People people= ac.getBean("peo", People.class);
  System.out.println(people);

测试结果:
在这里插入图片描述
注意一:如果设定的条件匹配多个有参构造方法,则执行最后的构造方法。
当类中的构造方法有多个时,如下所示:

 public People(int id, String name) {
  super();
  this.id = id;
  this.name = name;
  System.out.println("有参构造1");
 }
 public People(String name,int id) {
  super();
  this.id = id;
  this.name = name;
  System.out.println("有参构造2");
 }

在application中代码设置如下:

  <bean id="peo" class="com.bean.People" >
  <constructor-arg name="id" value="12" ></constructor-arg>
  <constructor-arg name="name" value="张三"></constructor-arg>
 </bean>

此时按照顺序执行最后一个有参构造方法。
执行结果如下所示:
在这里插入图片描述
如果要明确某一个有参构造方法,设置如下:
index确定第1个参数,name必须是id

 <bean id="peo" class="com.bean.People" >
  <constructor-arg index="0" name="id" value="12" ></constructor-arg>
  <constructor-arg index="1" name="name" value="张三"></constructor-arg>
 </bean>

注意二:
如果类中有以下构造参数:

 public People(int id, String name) {
  super();
  this.id = id;
  this.name = name;
  System.out.println("有参构造1");
 }
 public People(Integer id, String name) {
  super();
  this.id = id;
  this.name = name;
  System.out.println("有参构造2");
 }

则在applicationContext中设置其type,区分开两个构造参数,代码如下:

 <bean id="peo" class="com.bean.People" >
  <constructor-arg index="0" name="id" type="int" value="12" ></constructor-arg>
  <constructor-arg index="1" name="name" type="java.lang.String" value="张三"></constructor-arg>
 </bean>

第二种:通过实例工厂创建

需要先创建工厂,才能生产对象,一个工厂可以创建多个对象。
首先:创建工厂类

public class PeopleFactory {
 public People newInStance() {
  return new People();
 }
}

其次:在applicationContext设置bean

 <bean id="factory" class="com.bean.PeopleFactory"></bean>
 <bean id="people" factory-bean="factory" factory-method="newInStance"></bean>

最后:测试代码

  //不在applicationContext中配置bean时的测试代码
  PeopleFactory peopleFactory=new PeopleFactory();
  People people2= peopleFactory.newInStance();
  System.out.println(people2);
  //在applicationContext中配置bean时的测试代码
  People people=ac.getBean("people", People.class);
  System.out.println(people);

第三种:通过静态工厂创建

不需要创建工厂,可以快速创建对象。工厂不创建对象就可以调用方法的方式是:只需将方法变成static即可。
代码如下:

public class PeopleFactory {
 public static People newInStance() {
  return new People();
 }
}

在aaplicationContext中只需要配置工厂类和工厂类中的方法即可。

<bean id="people1" class="com.bean.PeopleFactory" factory-method="newInStance"></bean>
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值