1.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">
<!--如果需要创建多个对象,不想设置很多条,可以使用scope="prototype"设置为多实例的-->
<bean id="factoryBeanClass" class="test10month.test1012.FactoryBeanClass" scope="prototype"></bean>
</beans>
2.类
class FactoryBeanClass implements FactoryBean {
@Override
public Object getObject() throws Exception {
return null;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return false;
}
}
3.测试类
package test10month.test1012;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class FactoryBeanTest {
@Test
public void test() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("test10month/test1012/FactoryBeanTestSpring.xml");
var factoryBeanClass = context.getBean("factoryBeanClass", Object.class);
var factoryBeanClass2 = context.getBean("factoryBeanClass", Object.class);
var factoryBeanClass3 = context.getBean("factoryBeanClass", Object.class);
System.out.println(factoryBeanClass);
}
}