Java 4.Spring的对象创建的方法

一、无参构建函数

前面的方法调用了Person的无参构造函数创建了Person对象

二、有参构造函数

参考后面的依赖注入

三、实例工厂方法

工厂

	public Person createPerson2() {
		System.out.println("实例工厂创建person");
		return new Person();
	}

 配置文件

<!-- 实例工厂 -->
<bean name="personFactory" class="com.Spring.pojo.factory.PersonFactory" ></bean>
<bean name="person2" factory-method="createPerson2" ></bean>

 测试

扫描二维码关注公众号,回复: 8409056 查看本文章
	@Test
	public void testStaticFactroy2() {  
		@SuppressWarnings("resource")
		AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");		

	}

四、静态工厂方法

工厂

public class PersonFactory {
	public static Person createPerson1() {
		System.out.println("静态工厂创建person");
		return new Person();
	}

 配置文件

<bean name="person1" class="com.Spring.pojo.factory.PersonFactory" factory-method="createPerson1" ></bean>

 测试

	@Test
	public void testStaticFactroy() {
		@SuppressWarnings("resource")
		AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");		

	}

猜你喜欢

转载自www.cnblogs.com/yuzhenfu/p/12146455.html