网站建设学什么语音,做爰全过程免费的视频凤凰网站,专业网络推广方案,事业单位做网站需要前置审批吗一、概述
Spring中有2种类型的Bean#xff0c;一种是普通Bean#xff0c;另外一种是工厂Bean#xff08;FactoryBean#xff09;#xff1b;普通Bean#xff1a;在配置文件中定义的Bean的类型就是返回类型#xff1b;工厂Bean#xff1a;在配置文件中定义的Bean的类型…一、概述
Spring中有2种类型的Bean一种是普通Bean另外一种是工厂BeanFactoryBean普通Bean在配置文件中定义的Bean的类型就是返回类型工厂Bean在配置文件中定义的Bean的类型可以和返回类型不一样 1创建类让这个类作为工厂bean实现接口FactoryBean 2实现接口里边的方法在实现的方法中定义返回的Bean的类型
二、自定义Bean创建对象
// 工厂类
public class MyFactoryBean implements FactoryBeanCourse {Overridepublic Course getObject() throws Exception {Course course new Course(Springboot课程);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 idmyFactoryBean classorg.star.factory.MyFactoryBean/bean/beans// 测试
/*** Bean管理使用自定义Bean创建对象*/
Test
public void beanManagementTest8() {ClassPathXmlApplicationContext context new ClassPathXmlApplicationContext(applicationContext9.xml);Course course context.getBean(myFactoryBean, Course.class);System.out.println(course);
}
// 结果
Course(nameSpringboot课程)