【问题标题】:How can I override an EJB 3 session bean method with a generic argument - if possible at all? [closed]如果可能的话,如何使用通用参数覆盖 EJB 3 会话 bean 方法? [关闭]
【发布时间】:2008-08-18 15:34:34
【问题描述】:

假设您有以下 EJB 3 接口/类:

public interface Repository<E>
{
   public void delete(E entity);
}

public abstract class AbstractRepository<E>  implements Repository<E>
{
   public void delete(E entity){
      //...
   }
}

public interface FooRepository<Foo>
{
   //other methods
}

@Local(FooRepository.class)
@Stateless
public class FooRepositoryImpl extends
    AbstractRepository<Foo> implements FooRepository
{
   @Override
   public void delete(Foo entity){
      //do something before deleting the entity
      super.delete(entity);
   }
   //other methods
}

然后是另一个访问 FooRepository bean 的 bean:

//...
@EJB
private FooRepository fooRepository;

public void someMethod(Foo foo)
{
    fooRepository.delete(foo);
}
//...

但是,当调用FooRepository bean 的删除方法时,不会执行覆盖方法。相反,只执行AbstractRepository 中定义的删除方法的实现。

我做错了什么还是仅仅是 Java/EJB 3 的限制,泛型和继承还不能很好地结合在一起?

【问题讨论】:

  • 今天它在没有更改任何代码行的情况下工作。所以运行时环境,调试器或其他东西一定出了问题......感谢您的回答!

标签: java generics inheritance jakarta-ee ejb-3.0


【解决方案1】:

我用 pojo 试过了,它似乎有效。我不得不稍微修改你的代码。 我觉得你的界面有点不对劲,但我不确定。

我假设“Foo”是一个具体的类型,但如果不是,我可以为你做更多的测试。

我刚刚写了一个 main 方法来测试它。 我希望这会有所帮助!

public static void main(String[] args){
        FooRepository fooRepository = new FooRepositoryImpl();
        fooRepository.delete(new Foo("Bar"));
}

public class Foo
{
    private String value;

    public Foo(String inValue){
        super();
        value = inValue;
    }
    public String toString(){
        return value;
    }
}

public interface Repository<E>
{
    public void delete(E entity);
}

public interface FooRepository extends Repository<Foo>
{
    //other methods
}

public class AbstractRespository<E> implements Repository<E>
{
    public void delete(E entity){
        System.out.println("Delete-" + entity.toString());
    }
}

public class FooRepositoryImpl extends AbstractRespository<Foo> implements FooRepository
{
     @Override
       public void delete(Foo entity){
          //do something before deleting the entity
            System.out.println("something before");
          super.delete(entity);
       }
}

【讨论】:

    【解决方案2】:

    您能否仅将其用作 POJO 来针对您的 FooRepository 类编写单元测试。如果这按预期工作,那么我不熟悉它在容器内的功能会有所不同的任何原因。

    我怀疑还有其他事情发生,如果您将其作为 POJO 进行测试,可能会更容易调试。

    【讨论】:

      猜你喜欢
      • 2013-05-17
      • 1970-01-01
      • 2015-05-24
      • 2015-03-12
      • 1970-01-01
      • 1970-01-01
      • 2015-01-21
      • 1970-01-01
      • 2011-12-21
      相关资源
      最近更新 更多