从对象创建和引用小议解耦
2010-07-12 00:00:00 来源:WEB开发网清单 5. 计算器实现方式五 ( 对象注入 )
public class CalculatorUI {
private ExpressionEvaluator expressionEvaluator;
public CalculatorUI() {
}
public void setExpressionEvaluator(ExpressionEvaluator expressionEvaluator){
this.expressionEvaluator = expressionEvaluator;
}
......
}
Spring Configuration Fragment A (Use ExpressionEvaluatorImpl directly)
<beans>
<bean id="calculatorUI" class="CalculatorUI">
<property name="expressionEvaluator">
<ref bean="expressionEvaluator"/>
</property>
</bean>
<bean id="expressionEvaluator" class="ExpressionEvaluatorImpl"/>
</beans>
Spring Configuration Fragment B (Use ExpressionEvaluatorFactory)
<beans>
<bean id="calculatorUI" class="CalculatorUI">
<property name="expressionEvaluator">
<ref bean="expressionEvaluator"/>
</property>
</bean>
<bean id="expressionEvaluator" class="ExpressionEvaluatorFactory"
factory-method="createExpressionEvaluator"/>
</beans>
更多精彩
赞助商链接