【问题标题】:Is it necessary to add getter and setter at bean definition of osgi blueprint fileosgi蓝图文件的bean定义是否需要添加getter和setter
【发布时间】:2019-01-10 11:33:45
【问题描述】:

我的项目基于 Apache karaf osgi 和依赖注入是通过蓝图文件完成的。我想知道这些 bean 是否真的需要 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"/>

【问题讨论】:

  • 您似乎已经回答了自己的问题。您想知道是否真的需要 getter 和 setter……您尝试不使用它并且它有效。因此它们不是必需的。

标签: osgi apache-karaf karaf blueprint-osgi


【解决方案1】:

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

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

如果您希望您的蓝图容器定义可跨实现移植,那么最好使用 JavaBeans 样式的 setter 方法。如果您不关心这一点,那么您可以使用字段注入而忘记 setter 方法。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多