轉(zhuǎn)自:https://javadoop.com/post/spring-ioc
方法注入
一般來(lái)說(shuō),我們的應(yīng)用中大多數(shù)的 Bean 都是 singleton 的。singleton 依賴(lài) singleton,或者 prototype 依賴(lài) prototype 都很好解決,直接設(shè)置屬性依賴(lài)就可以了。
但是,如果是 singleton 依賴(lài) prototype 呢?這個(gè)時(shí)候不能用屬性依賴(lài),因?yàn)槿绻脤傩砸蕾?lài)的話(huà),我們每次其實(shí)拿到的還是第一次初始化時(shí)候的 bean。
一種解決方案就是不要用屬性依賴(lài),每次獲取依賴(lài)的 bean 的時(shí)候從 BeanFactory 中取。這個(gè)也是大家最常用的方式了吧。怎么取,我就不介紹了,大部分 Spring 項(xiàng)目大家都會(huì)定義那么個(gè)工具類(lèi)的。
另一種解決方案就是這里要介紹的通過(guò)使用 Lookup method。
lookup-method
我們來(lái)看一下 Spring Reference 中提供的一個(gè)例子:
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é)碼的方式來(lái)生成一個(gè)子類(lèi)。我們定義的類(lèi)不能定義為 final class,抽象方法上也不能加 final。
lookup-method 上的配置也可以采用注解來(lái)完成,這樣就可以不用配置 <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 只有一個(gè)實(shí)現(xiàn)類(lèi),那返回值也可以寫(xiě) Command。