【问题标题】:How To Self-Invoke a beans method in OSGI如何在 OSGI 中自调用 bean 方法
【发布时间】:2015-04-01 08:56:05
【问题描述】:
Class MyTestBean {
    public void requiredMethod(){
         //somehow call the "requiresNewMethod" with a new transaction
         //provided from the container
         this.requiresNewMethod(); //this does not work.. no new transaction is created            
     }

    public void requiresNewMethod(){ //code..}
}

蓝图 xml:

  <bean id="myTestBean "
    class="MyTestBean ">
    <jpa:context property="entityManager" unitname="sample" />
    <tx:transaction method="requiredMethod" value="Required" />
    <tx:transaction method="requiresNewMethod" value="RequiresNew" />
  </bean>

当 requiredMethod 被调用时,JPA 管理器创建一个新事务(如果它还没有在事务中)。我想从“requiredMethod”内部调用方法“requiresNewMethod”,并强制 JPA 管理器创建一个新的(嵌套)事务。

例如,如果上面是 EJB 并且我在 EJB 容器中,我可以这样做

public void requiredMethod(){
    sessionContext.getBusinessObject(MyTestBean.class).requiresNewMethod();
 }

【问题讨论】:

    标签: osgi apache-karaf blueprint-osgi


    【解决方案1】:

    您可以尝试将自己的 bean 注入到 MyTestBean 的属性中并以这种方式访问​​它。

    【讨论】:

    • 谢谢克里斯蒂安。我想要一个更清洁的解决方案。我不喜欢在 bean 中有那种循环引用。我会尝试你的建议,看看这是否可行,我会将它用作最后的解决方案。谢谢!
    • 原来将bean作为属性注入到bean本身中,然后通过注入的bean调用所需的方法是行不通的,行为与直接调用方法相同。另一方面,如果我创建一个新 bean 并注入,则可以作为魅力..但是创建一个新 bean 不是我想要的。谢谢
    猜你喜欢
    • 1970-01-01
    • 2015-06-29
    • 2011-12-23
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多