xml文件
<bean name="fs"
class="com.qq.pojo.StudentFactory"
factory-method="produce"></bean>
中间类
package com.qq.pojo;
public class StudentFactory {
public static Student produce(){
return new Student("小名",10,19);
}
}
实现类
@Test
public void test5(){
//<!--scope 设置是单例还是非单例-->
//scope="prototype" 表示多例模式
ClasspathXmlApplicationContext context = new ClasspathXmlApplicationContext("application-context.xml");
Student s1 = (Student) context.getBean("fs");
/*Student bean1 = context.getBean(Student.class);
System.out.println(bean==bean1);
System.out.println(bean);
System.out.println(bean1);*/
System.out.println(s1);
}
非静态方法
<bean id="maker" class="com.qq.pojo.StudenetMaker"></bean>
<bean name="ms" factory-bean="maker" factory-method="make"></bean>
工厂类
class StudenetMaker{
public Student make(){
return new Student("tom",10,19);
}
}
测试类
public void test6(){
//<!--scope 设置是单例还是非单例-->
//scope="prototype" 表示多例模式
ClasspathXmlApplicationContext context = new ClasspathXmlApplicationContext("application-context.xml");
Student s1 = (Student) context.getBean("ms");
/*Student bean1 = context.getBean(Student.class);
System.out.println(bean==bean1);
System.out.println(bean);
System.out.println(bean1);*/
System.out.println(s1);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。