如果可能的话,如何使用通用参数重写 EJB 3 会话 bean 方法?[关闭]
-
08-06-2019 - |
题
假设您有以下 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
}
然后另一个访问的 bean FooRepository
豆 :
//...
@EJB
private FooRepository fooRepository;
public void someMethod(Foo foo)
{
fooRepository.delete(foo);
}
//...
但是,当删除方法时,重写方法永远不会被执行。 FooRepository
豆被称为。相反,仅实现定义在 AbstractRepository
被执行。
我做错了什么,或者这只是 Java/EJB 3 的限制,泛型和继承还不能很好地协同工作?
解决方案
我用 pojo 尝试了一下,似乎有效。我不得不稍微修改一下你的代码。我认为你的界面有点不对劲,但我不确定。
我假设“Foo”是一个具体类型,但如果不是,我可以为您做更多测试。
我刚刚写了一个主要方法来测试这一点。我希望这有帮助!
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);
}
}
其他提示
您可以针对您的 FooRepository 类编写一个单元测试吗?仅将其用作 POJO。如果它按预期工作,那么我不熟悉它在容器内以不同方式运行的任何原因。
我怀疑还有其他事情发生,如果您将其作为 POJO 进行测试,调试可能会更容易。
不隶属于 StackOverflow