免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
ビーンをインスタンス化する方法の三種類
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被实例化了...");
}
}
デモ:
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");
}
}