轉(zhuǎn)自:https://javadoop.com/post/spring-ioc
方法注入
一般來說,我們的應(yīng)用中大多數(shù)的 Bean 都是 singleton 的。singleton 依賴 singleton,或者 prototype 依賴 prototype 都很好解決,直接設(shè)置屬性依賴就可以了。
但是,如果是 singleton 依賴 prototype 呢?這個時候不能用屬性依賴,因為如果用屬性依賴的話,我們每次其實拿到的還是第一次初始化時候的 bean。
一種解決方案就是不要用屬性依賴,每次獲取依賴的 bean 的時候從 BeanFactory 中取。這個也是大家最常用的方式了吧。怎么取,我就不介紹了,大部分 Spring 項目大家都會定義那么個工具類的。
另一種解決方案就是這里要介紹的通過使用 Lookup method。
lookup-method
我們來看一下 Spring Reference 中提供的一個例子:
package fiona.apple;
public abstract class CommandManager {
public Object process(Object commandState) {
// grab a new instance of the appropriate Command interface
Command command = createCommand();
// set the state on the (hopefully brand new) Command instance
command.setState(commandState);
return command.execute();
}
// okay... but where is the implementation of this method?
protected abstract Command createCommand();
}
xml 配置 <lookup-method />:
<!-- a stateful bean deployed as a prototype (non-singleton) -->
<bean id="myCommand" class="fiona.apple.AsyncCommand" scope="prototype">
<!-- inject dependencies here as required -->
</bean>
<!-- commandProcessor uses statefulCommandHelper -->
<bean id="commandManager" class="fiona.apple.CommandManager">
<lookup-method name="createCommand" bean="myCommand"/>
</bean>
Spring 采用 CGLIB 生成字節(jié)碼的方式來生成一個子類。我們定義的類不能定義為 final class,抽象方法上也不能加 final。
lookup-method 上的配置也可以采用注解來完成,這樣就可以不用配置 <lookup-method /> 了,其他不變:
public abstract class CommandManager {
public Object process(Object commandState) {
MyCommand command = createCommand();
command.setState(commandState);
return command.execute();
}
@Lookup("myCommand")
protected abstract Command createCommand();
}
注意,既然用了注解,要配置注解掃描:<context:component-scan base-package=”com.javadoop” />
甚至,我們可以像下面這樣:
public abstract class CommandManager {
public Object process(Object commandState) {
MyCommand command = createCommand();
command.setState(commandState);
return command.execute();
}
@Lookup
protected abstract MyCommand createCommand();
}
上面的返回值用了 MyCommand,當(dāng)然,如果 Command 只有一個實現(xiàn)類,那返回值也可以寫 Command。