网站广告调词平台,首页模板,泉州网站建设有哪些,如何下载和安装wordpressIOC操作Bean管理#xff08;FactoryBean#xff09;
1.Spring有两种类型bean#xff0c;一种普通bean#xff0c;另外一种工厂bean#xff08;FactoryBean#xff09;
2.普通bean#xff1a;在配置文件中定义bean类型就是返回类型
3.工厂bean#xff1a;在配置文件定…IOC操作Bean管理FactoryBean
1.Spring有两种类型bean一种普通bean另外一种工厂beanFactoryBean
2.普通bean在配置文件中定义bean类型就是返回类型
3.工厂bean在配置文件定义bean类型可以和返回类型不一样
第一步 创建类让这个类作为工厂bean实现接口FactoryBean
第二步 实现接口里面的方法在实现的方法中定义返回的bean类型
package com.atguigu.spring.collectiontype;public class Course {private String cname;public void setCname(String cname) {this.cname cname;}
}
package com.atguigu.spring.factoryBean;import com.atguigu.spring.collectiontype.Course;
import org.springframework.beans.factory.FactoryBean;public class MyBean implements FactoryBeanCourse {//定义返回beanOverridepublic Course getObject() throws Exception {Course course new Course();course.setCname(abc);return course;}Overridepublic Class? getObjectType() {return null;}Overridepublic boolean isSingleton() {return false;}
}
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean id myBean class com.atguigu.spring.factoryBean.MyBean/bean/beans测试
package com.atguigu.spring.test;import com.atguigu.spring.collectiontype.Book;
import com.atguigu.spring.collectiontype.Course;
import com.atguigu.spring.factoryBean.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class testBook {Testpublic void testCollection2(){ApplicationContext context new ClassPathXmlApplicationContext(bean3.xml);Course myBean context.getBean(myBean, Course.class);System.out.println(myBean);}}