IOC Operation Bean Management (FactoryBean)
The following is the learning before Bean management (factory bean), injecting collections based on xml and implementing
Inject collection based on xml and implement: http://t.csdn.cn/H0ipR
Spring has two types of beans, a normal bean and a factory bean (FactoryBean)
Ordinary bean: the bean type defined in the configuration file is the return type
As follows, in the common type, the type of book is defined, then the returned book must be of what type
<bean id="book" class="com.atguigu.spring5.collectiontype.Book">
<property name="list" ref="booklist"></property>
</bean>
Factory bean: The bean type defined in the configuration file can be different from the return type.
The first step is to create a class, let this class be a factory bean, and implement the interface FactoryBean
The second step is to implement the method in the interface, and define the returned bean type in the implemented method
public class MyBean implements FactoryBean<Course> {
//定义返回bean
@Override
public Course getObject() throws Exception {
Course course = new Course();
course.setCname("abc");
return course;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return FactoryBean.super.isSingleton();
}
}
test class
@Test
public void testCollection3(){
ApplicationContext context =
new ClassPathXmlApplicationContext("bean3.xml");
Course course = context.getBean("myBean", Course.class);
System.out.println(course);
}
xml
<bean id="myBean" class="com.atguigu.spring5.FactoryBean.MyBean"></bean>