使用Spring(二)实例化方式(静态工厂,实例工厂)
来源:互联网 发布:学摄影的书籍知乎 编辑:程序博客网 时间:2024/05/22 03:41
1.applicationContext.xml
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- <bean id="dogBean" class="com.yw.test02.Dog"> </bean> --><!-- <alias name="dogBean" alias="dogBean3" /> --><!-- createInstance()必须是一个static方法。 --><bean id="exampleBean" class="com.yw.test02.Dog" factory-method="createInstance" /><!-- the factory bean, which contains a method called createInstance() --><bean id="myFactoryBean" class="com.yw.test02.Dog"></bean><!-- the bean to be created via the factory bean --><bean id="exampleBean2" factory-bean="myFactoryBean"factory-method="createInstance2" /></beans>
2.测试类
package com.yw.test02;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.FileSystemResource;import org.springframework.core.io.Resource;public class Test02{public static void main(String[] args){Resource is = new FileSystemResource("src/com/yw/test02/applicationContext3.xml");BeanFactory factory = new XmlBeanFactory(is);//使用 静态工厂方法实例化Object obj1=factory.getBean("exampleBean");//使用实例工厂方法实例化Object obj2=factory.getBean("exampleBean2");System.out.println("obj1="+obj1);System.out.println("obj2="+obj2);}}
3.Dog类
package com.yw.test02;public class Dog{private String name;public String getName(){return name;}public void setName(String name){this.name = name;}public static Object createInstance(){System.out.println("==public static void createInstance()==");return "createInstance()返回值";}public Object createInstance2(){System.out.println("==public void createInstance2()==");return "createInstance2返回值";}@Overridepublic String toString(){return "Dog [name=" + name + "]";}}
4.运行如图
0 0
- 使用Spring(二)实例化方式(静态工厂,实例工厂)
- Spring 使用静态工厂方式实例化Bean
- spring静态工厂实例化
- Spring 静态工厂和实例工厂
- spring 静态工厂 &实例工厂实例方法 & 实现factorybean接口
- Spring 使用工厂方法实例化对象
- Spring的实例工厂方法和静态工厂方法
- spring的静态工厂和实例工厂方法
- Spring的实例工厂方法和静态工厂方法
- 静态工厂实例代码
- Spring 使用实例工厂方法实例化Bean
- Spring使用实例工厂方法实例化Bean
- Spring Bean工厂创建Bean实例的方法之使用静态工厂类
- Spring学习之使用静态工厂方法以及实例工厂方法配置bean
- Spring工厂方式创建Bean实例
- Spring 工厂方式 创建bean实例
- spring学习小结7_使用工厂方法来配置bean:静态工厂方法和实例工厂方法
- spring 实例工厂
- MFC文档/视图结构体系及SDI回顾(2)
- 程序员最害怕的5件事,你中招了吗?
- destoon通过修改源代码实行及时订单处理在线支付
- 安装jpype出现_jpype错误的解决方案
- equals与==之间的区别
- 使用Spring(二)实例化方式(静态工厂,实例工厂)
- The Log(我所读过的最好的一篇分布式技术文章)
- 从“鞭打快牛”故事来看团队的领导力
- Codeforces Round #377 (Div. 2)题解报告
- 汇编随笔
- 动态规划之最长回文子字符串(Longest Palindromic Substring)
- PHP+MySQL查询,Unexpected taken s 错误解决方法
- 第八周项目1-建立顺序串的算法库
- 驱动中获取进程完整路径名