对象创建三法
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();
}
}
运行结果