7.普通bean和FactoryBean的区别
普通bean和FactoryBean的区别:前者定义什么类型就返回什么类型,后者不同
Teacher类
package Spring5.Student;
public class Teacher {
}
Student类
package Spring5.Student;
import org.springframework.beans.factory.FactoryBean;
public class Student implements FactoryBean<Teacher> {
@Override
public boolean isSingleton() {
return FactoryBean.super.isSingleton();
}
@Override
public Teacher getObject() throws Exception {
Teacher t=new Teacher();
return t;
}
@Override
public Class<?> getObjectType() {
return null;
}
}
bean6.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="Student" class="Spring5.Student.Student">
</bean>
</beans>
测试类
public class TEST {
//普通bean和FactoryBean的区别:前者定义什么类型就返回什么类型,后者不同
@Test
public void testBean6() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean6.xml");
Teacher t = context.getBean("Student", Teacher.class);
System.out.println(t);
}
}
测试结果
Spring5.Student.Teacher@79924b