spring-02-对象创建三法

本文详细介绍了Spring框架中创建对象的三种方法:构造器创建、静态工厂创建与普通工厂创建,以及如何通过配置XML文件实现这些创建过程。同时,通过具体示例展示了不同创建方式的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对象创建三法

1、构造器创建
appContext.getBean(Class/“id”);
Spring默认调用类的“无参构造方法”来创建对象。

2、静态工厂创建&普通工厂
静态工厂,里面写一个静态方法返回一个类的实例,普通工厂就把静态关键字去掉

public class 静态工厂 {
	public 静态工厂(){
		System.out.println("====="+getClass());
	}
	static Cat createObj(){
		return  new Cat();
	}
	
}
------------------------------------------------------------
public class 普通工厂 {
	public 普通工厂(){
		System.out.println("==="+getClass());
	}
	Cat createObj(){
		return  new Cat();
	}
	

}

3、配置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_static" class="spring02.静态工厂"  factory-method="createObj" />
	
	//普通工厂方法
	<bean id = "factory_common" class="spring02.普通工厂"/>
	<bean id="prodect1" factory-bean="factory_common" factory-method="createObj"></bean>

</beans>

4、测试
——静态工厂使用的工厂的的id获取对象,而普通工厂使用的工厂下面的方法的id获取对象 ;

public class Test02 {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("spring02/applicationContext.xml");
		System.out.println("----------------");
		System.out.println(ac);
		//静态工厂,使用工厂的id获取对象(工厂不会实例化)
		Cat cat =(Cat)ac.getBean("factory_static");
		System.out.println(cat);
		cat.speak();
		
		//普通工厂,使用产品的ID获取对象(工厂不会实例化)
		Cat cat2 = (Cat)ac.getBean("prodect1");
		System.out.println(cat2);
		cat2.speak();
		
	}

}

运行结果:从结果上看静态工厂没有实例化
在这里插入图片描述

练习

富士康代工生产小米和苹果,乐视生产汽车

public class MI {
	public void slogan(){
		System.out.println("为发烧而生!");
	}

}
--------------------------------------------------------
public class Iphone {
	public void slogan(){
		System.out.println("唯一的不同就是处处不同!");
	}
}
--------------------------------------------------------
public class Car {
	public void go(){
		System.out.println("跑起来!");
	}

}

创建工厂

public class Foxconn {
	public Iphone productionIphone(){
		return new Iphone();
	}
	public MI productionMi(){
		return new MI();
	}
}
--------------------------------------------------------
public class LeEco {
	public static Car productionCar(){
		return new Car();
	}

}

配置 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="foxconn" class="spring02Test.Foxconn" />
	<!-- 普通工厂:产品 -->
	<bean id="iphone" factory-bean="foxconn" factory-method="productionIphone" />
	<bean id="mi" factory-bean="foxconn" factory-method="productionMi" />
	
	<!-- 静态工厂 -->
	<bean id="leeco" class="spring02Test.LeEco" factory-method="productionCar" />

</beans>

测试

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

public class Test01 {
	
	@Test
	public void test(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("spring02Test/applicationContext.xml");
		 Iphone iphone = (Iphone) ac.getBean("iphone");
		 iphone.slogan();
		 MI mi = (MI) ac.getBean("mi");
		 mi.slogan();
		 
		Car car = (Car) ac.getBean("leeco");
		car.go();
		 
		
	}

}

运行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值