标签:vat enc owa war 工厂模式 work pack hid new
IOC 概念
IOC即控制反转,其借鉴了工厂模式的思想,把实例化对象的代码抽取出来封装到一个地方统一管理。工厂模式是集中到工厂类里统一管理,spring是集中到xml配置文件里统一管理。
demo
package com.test.spring; // 汉武帝 public class HanWudi { // 将军 private General general; public HanWudi(General general) { this.general = general; } // order 命令 public void order() { general.goToWar("楼兰"); } }
package com.test.spring; // 将军 public interface General { public void goToWar(String placeName); // 地方 }
package com.test.spring; // 霍去病 public class HuoQubing implements General { @Override public void goToWar(String placeName) { System.out.println("go to war with " + placeName); } }
<?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 id="king" class="com.test.spring.HanWudi"> <constructor-arg ref="general"/> </bean> <bean id="general" class="com.test.spring.HuoQubing"></bean> </beans>
package com.test.spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Driver { private HanWudi hanWudi; public static void main(String[] args) { // my.xml放在maven项目的resources目录下 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/my.xml"); HanWudi hanWudi = context.getBean(HanWudi.class); hanWudi.order(); context.close(); } }
标签:vat enc owa war 工厂模式 work pack hid new
原文地址:https://www.cnblogs.com/Mike_Chang/p/10245893.html