版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
三种实例化Bean的方式
1.使用类构造器实例化( 默认无参数)
2.使用静态工厂方法实例化(简单工厂模式)
3.使用实例工厂方法实例化(工厂方法模式)
代码演示
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实例化三种方式 -->
<!-- 第一种:.使用类构造器实例化( 默认无参数) -->
<bean id="bean1" class="com.shunli.ioc.demo1.Bean1"></bean>
<!-- 第二种:使用静态工厂方法实例化(简单工厂模式) -->
<bean id="bean2" class="com.shunli.ioc.demo1.Bean2Factory" factory-method="createBean2"></bean>
<!-- 第三种:使用实例工厂方法实例化(工厂方法模式) -->
<bean id="bean3Factory" class="com.shunli.ioc.demo1.Bean3Factory"></bean>
<bean id="bean3" factory-bean="bean3Factory" factory-method="createBean3"></bean>
</beans>
第一种:.使用类构造器实例化( 默认无参数)
package com.shunli.ioc.demo1;
/**
* Bean的实例化三中方式:1.使用类构造器实例化( 默认无参数)
* @author shunli
*
*/
public class Bean1 {
public Bean1() {
System.out.println("Bean1被实例化了...");
}
}
demo:
package com.shunli.ioc.demo1;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Bean实例化三种方式:1.使用类构造器实例化( 默认无参数)
* @author shunli
*
*/
public class TestMain {
@Test
public void demo1() {
//创建工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂获得类的实例
Bean1 bean1 = (Bean1)applicationContext.getBean("bean1");
}
}
第二种:.使用静态工厂方法实例化(简单工厂模式)
package com.shunli.ioc.demo1;
/**
* Bean的实例化三中方式:2、使用静态工厂方法实例化(简单工厂模式)
* @author shunli
*
*/
public class Bean2 {
}
package com.shunli.ioc.demo1;
/**
* Bean2的静态工厂
* @author shunli
*
*/
public class Bean2Factory {
public static Bean2 createBean2() {
System.out.println("Bean2Factory");
return new Bean2();
}
}
demo中:
package com.shunli.ioc.demo1;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Bean实例化三种方式:2、使用静态工厂方法实例化(简单工厂模式)
* @author shunli
*
*/
public class TestMain {
@Test
public void demo2() {
//创建工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂获得类的实例
Bean2 bean2 = (Bean2)applicationContext.getBean("bean2");
}
}
第三种、使用实例工厂方法实例化(工厂方法模式)
package com.shunli.ioc.demo1;
/**
* Bean的实例化三中方式:3、使用实例工厂方法实例化(工厂方法模式)
* @author shunli
*
*/
public class Bean3 {
}
package com.shunli.ioc.demo1;
/**
* Bean3实例工厂
* @author shunli
*
*/
public class Bean3Factory {
public Bean3 createBean3() {
System.out.println("Bena3工厂被执行了");
return new Bean3();
}
}
package com.shunli.ioc.demo1;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Bean实例化三种方式
* @author shunli
*
*/
public class TestMain {
@Test
public void demo3() {
//创建工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂获得类的实例
Bean3 bean3 = (Bean3)applicationContext.getBean("bean3");
}
}