1.Spring里面有两种Bean一种是普通bean,一种是工厂bean(FactoryBean)
主要区别:普通bean:定义什么类型,返回就是什么类型
工厂bean:定义什么类型,可以返回其他类型
主要实现就是通过设置一个类继承FactoryBean接口
2.工厂bean的实现
第一步 创建类,让这个类成为工厂bean,实现接口FactoryBean
第二步 实现接口里面的三个方法,只用更改其中的返回值类型的方法,
在xml中配置
测试输出
@Test public void test(){ //factorybean 返回类型可以和class中的不一样 ApplicationContext context = new ClassPathXmlApplicationContext("beanXml/bean3.xml"); Course course = context.getBean("mybean",Course.class); System.out.println(course); }