1.工厂模式就是指隐藏创建类的细节,通过一个额外的工厂类来组织创建我们需要的对象,工厂按表现形式又分为静态工厂和工厂实例
2. src/main/java/com/imooc/spring/ioc/factory/AppleStaticFactory.java 苹果的静态工厂
package com.imooc.spring.ioc.factory; import com.imooc.spring.ioc.entity.Apple; /** * 静态工厂通过静态方法创建对象,隐藏对象细节 */ public class AppleStaticFactory { public static Apple createSweetApple() { Apple apple = new Apple(); apple.setTitle("红富士"); apple.setOrigin("欧洲"); apple.setColor("红色"); return apple; } }
2.1. src/main/resources/applicationContext.xml
<!--利用静态工厂获取对象--> <bean id="apple4" class="com.imooc.spring.ioc.factory.AppleStaticFactory" factory-method="createSweetApple"/>
标签:XML,基于,静态方法,Apple,spring,工厂,imooc,apple From: https://www.cnblogs.com/LLL0617/p/16913875.html