这里写目录标题
createBean中 replace/lookup-method创建bean实例
作用
根据 spring生命周期创建的bean A,使用@Autowire/@Resource属性注入 bean B,即使bean B的scope属性为prototype,在A中每次使用的 B都是同一个,因为在 A初始化时,属性已经和 B实例绑定。
保证单例bean 可以引用 原型bean。
lookup-method 实例
前期准备bean
public class Student {
}
public class Teacher {
}
// 此处,如果student和teacher可继承自定义Person,抽象方法可以返回可改为Person
public abstract class Staff {
public abstract Object getObject();
}
xml文件中添加 lookup-method属性
<?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">
<bean id="student" class="com.spring.learn.common.staff.Student" scope="prototype"/>
<bean id