是否有必要在osgi蓝图文件的bean定义中添加getter和setter

时间:2019-01-10 11:33:45

标签: osgi apache-karaf karaf blueprint-osgi

我基于Apache karaf osgi和依赖项注入的项目是通过蓝图文件完成的。我想知道此类豆是否真的需要getter和setter。我在没有getter和setter方法的情况下对其进行了测试,并且它可以工作,但不确定是否遵循最佳实践。我的动机是只是减少该文件的LOC。

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"..>
.
.
<bean id="emailServiceImpl" class="com.mycompany.EmailServiceImpl">
  <property name="applicationEnvironment" value="$(staging)" />
.
.
<bean id="orderDispatcherImpl" class="com.myCompany.OrderDispatcherImpl" 
  ext:field-injection="true" init-method="init">
   <property name="emailService" ref="emailServiceImpl"/>

1 个答案:

答案 0 :(得分:1)

Blueprint specification仅定义通过JavaBeans风格的setter方法注入属性。

字段注入是一个扩展,特定于Karaf中使用的Blueprint的Apache Aries实现。因此,它将无法在其他蓝图实现中使用。

如果希望您的蓝图容器定义可在各种实现之间移植,则最好使用JavaBeans风格的setter方法。如果您对此不关心,则可以使用字段注入,而无需使用setter方法。

但是,请注意,保留设置方法的另一个原因可能是出于单元测试的目的。

相关问题