Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Three kinds of ways to instantiate Bean
1. instantiated class constructor (no default parameter)
2. Example of a static factory method (simple factory pattern)
3. Examples of the method of Example plant (factory methods)
Code shows
applicationContext.xml configuration is as follows:
<?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>
First: use the instance of the class constructor (no default parameter)
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");
}
}
The second: using a static factory method to instantiate (simple factory pattern)
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();
}
}
In the 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");
}
}
Third, the method of Example of Example plant (factory methods)
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");
}
}