008.初始化IoC容器(基于XML配置Bean 基于工厂实例化对象,工厂实例方法)05
1. 工厂实例方法创建对象是指Ioc容器对工厂类进行实例化,并调用对应的实例方法创建对象的过程
2. src/main/java/com/imooc/spring/ioc/factory/AppleFactoryInstance.java
package com.imooc.spring.ioc.factory; import com.imooc.spring.ioc.entity.Apple; /** * 工厂实例方法创建对象是指Ioc容器对工厂类进行实例化并调用对应的实例方法创建对象的过程 */ public class AppleFactoryInstance { public Apple createSweetApple() { Apple apple = new Apple(); apple.setTitle("红富士"); apple.setOrigin("欧洲"); apple.setColor("红色"); return apple; } }
3. src/main/resources/applicationContext.xml
<!--利用工厂实例化方法获取对象-->
<bean id="factoryInstance" class="com.imooc.spring.ioc.factory.AppleFactoryInstance"/>
<bean id="apple5" factory-bean="factoryInstance" factory-method="createSweetApple"/>