Spring 一二事(2)
2016-03-22 10:50
417 查看
静态工厂方法及实例工厂的使用:
applicationContext.xml:
HelloWorldFactory.java
HelloWorldFactory2.java
测试:
github地址:https://github.com/leechenxiang/maven-spring001-helloworld
applicationContext.xml:
<!-- factory-method 是指调用静态工厂方法 --> <bean id="helloWorld2" class="com.lee.spring002.createobject.method.HelloWorldFactory" factory-method="getInstance"></bean> <!-- 实例工厂 --> <bean id="helloWorldFactory" class="com.lee.spring002.createobject.method.HelloWorldFactory2"></bean> <!-- factory-bean 是一个工厂bean --> <bean id="helloWorld3" factory-bean="helloWorldFactory" factory-method="getInstance"></bean>
HelloWorldFactory.java
package com.lee.spring002.createobject.method; import com.lee.spring001.createobject.HelloWorld; public class HelloWorldFactory { public static HelloWorld getInstance() { return new HelloWorld(); } }
HelloWorldFactory2.java
package com.lee.spring002.createobject.method; import com.lee.spring001.createobject.HelloWorld; public class HelloWorldFactory2 { public HelloWorld getInstance() { return new HelloWorld(); } }
测试:
@Test public void testHelloWorld_StaticFactory() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld hello = (HelloWorld)context.getBean("helloWorld2"); hello.hello(); } @Test public void testHelloWorld_InstanceFactory() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld hello = (HelloWorld)context.getBean("helloWorld3"); hello.hello(); }
github地址:https://github.com/leechenxiang/maven-spring001-helloworld
相关文章推荐
- eclipse启动无响应,停留在Loading状态
- java基础 - 位运算符
- Leetcode: 165. Compare Version Numbers(JAVA)
- 对Java多态性的理解
- Struts 有哪些经常使用标签库
- wget 官方jdk
- Spring Security(06)——AuthenticationProvider
- Spring MVC 流程图
- eclipse 安装svn插件
- java基础第四天_复习运算符、进制转化和数组
- 给Java说句公道话
- Spring的AOP使用xml配置
- Eclipse 中的重构功能
- Java的字节流和字符流
- JavaScrit常用的简单交互
- Java数组取交集
- Java内存区域
- Java中父类与子类的普通代码块,构造代码块,静态代码块 初始化
- Spring 一二事(1)
- Spring MVC with Maven - java.lang.ClassNotFoundException: org.springframework.web.servlet.Dispatcher