Spring依赖注入的三种方式详解之三:工厂方法注入
2016-12-29 00:00
1301 查看
spring框架提供了三种方式的基于xml配置依赖注入:属性注入,构造方法注入,工厂方法注入。本文举例演示工厂方法注入。
例如有类MasterC
package com.bwf51coding.bean; public class MasterC { private int age; private String name; private MasterC(int age, String name) { this.age=age; this.name=name; } private static MasterC c; public static MasterC getInstance(int age, String name){ if(c==null){ c=new MasterC(age, name); } return c; } @Override public String toString() { return "MasterC [age=" + age + ", name=" + name + "]"; } } |
<bean id="masterc" class="com.bwf51coding.bean.MasterC" factory-method="getInstance" > <constructor-arg value="40"/> <constructor-arg value="Alice"/> </bean> |
package com.bwf51coding.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bwf51coding.bean.MasterC; public class TestC { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); MasterC masterc=(MasterC)ac.getBean("masterc"); System.out.println(masterc); } } |
相关文章推荐
- Spring依赖注入的三种方式详解之三:工厂方法注入
- Spring依赖注入的三种方式详解之二:构造方法注入
- Spring依赖注入的三种方式详解之二:构造方法注入
- Spring依赖注入的三种方式详解之一:属性注入
- Spring依赖注入的三种方式详解之一:属性注入
- Spring依赖注入的三种方式实例详解
- Spring依赖注入的三种方式详解之一:属性注入
- spring Ioc 依赖注入的三种方式:构造函数注入、setter方法注入和接口注入
- Spring依赖注入的三种方式详解之一:属性注入
- SSH深度历险记(八) 剖析SSH核心原则+Spring依赖注入的三种方式
- Spring的依赖注入(DI)三种方式
- (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)
- Spring-三种依赖注入方式
- SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式
- spring的依赖注入方式详解
- 【SSH系列】深入浅出spring IOC中三种依赖注入方式
- spring依赖注入的好处以及三种注入的方式
- 【Spring】依赖注入三种方式
- SSH深度历险(八) 剖析SSH核心原理+Spring依赖注入的三种方式
- spring四种依赖注入方式 (set,构造器,工厂,注解 )