Spring框架创建对象的三种方式

引言

今天给大家讲解一下Spring框架创建对象的三种方式

创建方式

1.通过构造方法创建。它分为两种,一种是使用无参构造,另一种则是使用有参构造

2.工厂设计模式中的实例工厂

3.工厂设计模式中的静态工厂

具体内容

1.使用构造方法来创建对象。在默认情况下执行的是无参构造,这里就不说了。在这我们讲的如何使用有参构造

实体类

package a.b.pojo;

public class People {
	private int id;
	private String name;
	

	public People() {
		super();
		System.out.println("执行构造方法");
	}
	public People(int id, String name) {
		super();
		this.id = id;
		this.name = name;
		System.out.println("执行有参构造");
	}
	public People(Integer id, String name) {
		super();
		this.id = id;
		this.name = name;
		System.out.println("执行有参构造Integer");
	}
	public People(String name, int id) {
		super();
		this.id = id;
		this.name = name;
		System.out.println("执行有参构造1111111");
	}
	
	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 "People [id=" + id + ", name=" + name + "]";
	}
}

applicationContext.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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- index:参数的索引
         name:参数名
         type:类型(区分开关键字和封装类.比如int和Integer)
    -->
    <bean id="peo" class="a.b.pojo.People">
    	<!-- ref引用另一个bean   value 基本数据类型或String等 -->
    	<constructor-arg index="0" name="id" type="int" value="123"></constructor-arg>
    	<constructor-arg index="1" name="name" type="java.lang.String" value="张三"></constructor-arg>
    </bean>
</beans>

测试

package a.b.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import a.b.pojo.People;

public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		People people = ac.getBean("peo",People.class);
		System.out.println(people);
	}
}

注意:使用有参构造时,必须要在类中提供有参构造的方法。并且要在配置文件中设置调用哪个构造方法来帮助我们创建对象,如果设置的条件同时匹配多个构造方法的话,程序会执行最后的构造方法。

2.工厂设计模式中的实例工厂

在这里给大家多提一嘴,工厂设计模式大家都很清楚。它的主要作用就是帮助我们来创建类的对象的。任何需要生成复杂对象的地方,都可以用到工厂模式,而且一个工厂同时还可以生产多个对象。同时还有什么实例工厂、静态工厂、简单工厂、抽象工厂等等很多······这些就需要大家私下多了解

今天我们在Spring框架中创建对象用的是实例工厂,用一句话来概括就是需要先创建工厂,然后才能够生产对象。步骤如下:

首先必须要有一个实例工厂

public class PeopleFactory {
	public People newInstance(){
		return new People(1,"测试");
	}
}

其次,在applicationContext.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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="factory" class="a.b.pojo.PeopleFactory"></bean>
    <bean id="peo1" factory-bean="factory" factory-method="newInstance"></bean>
</beans>

最后测试

package a.b.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import a.b.pojo.People;
import a.b.pojo.PeopleFactory;

public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		People people = ac.getBean("peo1",People.class);
		System.out.println(people);
		
		/**
		 * 实例工厂测试代码
		 * 
		 * 具体如何实例化工厂和本个知识点无关系.
		 * 
		 */
//		PeopleFactory factory = new PeopleFactory();
//		People people = factory.newInstance();
	}
}

3.工厂设计模式中的静态工厂

它相比实例工厂不同的是,它可以不需要创建工厂,就可以快速的创建对象。步骤如下:

在工厂类中添加一个static,它就变成了静态

public class PeopleFactory {
	public static People newInstance(){
		return new People(1,"测试");
	}
}

而后在applicationContext.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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- <bean id="factory" class="a.b.pojo.PeopleFactory"></bean>
    <bean id="peo1" factory-bean="factory" factory-method="newInstance"></bean> -->
    
    <bean id="peo2" class="a.b.pojo.PeopleFactory" factory-method="newInstance"></bean>
</beans>

最后测试

package a.b.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import a.b.pojo.People;
import a.b.pojo.PeopleFactory;

public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		People people = ac.getBean("peo2",People.class);
		System.out.println(people);
		
		/**
		 * 静态工厂
		 */
//		People peo = PeopleFactory.newInstance();
	}
}
以上就是Spring框架中创建对象的三种方式

猜你喜欢

转载自blog.csdn.net/let_me/article/details/81012711