Java CDI:具有多个通用参数的装饰器

2022-09-04 04:47:41

我有以下结构:

@Decorator
public abstract class MyDecorator<T extends BaseEntity, Q extends QueryParams> implements EntityService<T, Q> {

    @Any
    @Inject
    @Delegate
    EntityService<T, Q> delegate;

    @Override
    public T save(T entity) { ... }

} 

这是接口声明:EntityService

public interface EntityService<T extends BaseEntity, Q extends QueryParams> {

    T save(T entity);

    void deleteById(Integer id);

    void deleteAllById(List<Integer> ids);

    void delete(T entity);

    void deleteAll(List<T> entities);

    T findById(Integer id);

    QueryResultWrapper<T> query(Q parameters);

    Long count(Q parameters);

}

不幸的是,装饰器 save 方法永远不会在应该调用的时候调用,尽管没有显示任何错误...我让它工作的唯一方法是这样的:

@Decorator
public abstract class MyDecorator<T extends BaseEntity> implements EntityService<T> { ... }

没有通用参数。Q extends QueryParams

在 内部声明 。MyDecoratorbeans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
   bean-discovery-mode="all" version="1.1">

    <decorators>
        <class>fortuna.backend.comum.decorators.MyDecorator</class>
    </decorators>

</beans>

有什么线索吗?


答案 1

设法解决了这个问题。我的问题是我直接在大多数端点/服务实现中使用,例如:QueryParams

public class PersonService extends EntityService<Person, QueryParams> { ... }

其实不然,其实是类本身!这就是为什么根本没有触发的原因!QueryParamsextends QueryParamsPersonServiceMyDecorator

因此,我创建了一个名为并使用它的接口,如下所示:IQueryParams

public abstract class MyDecorator<T extends BaseEntity, Q extends IQueryParams> implements EntityService<T, Q> {

现在 save 方法会触发 .PersonServiceMyDecorator


答案 2

您正在执行此操作 ->

EntityService<T extends BaseEntity, Q extends QueryParams>

如果该接口采用两个 Object,一个 T 扩展 BaseEntity,另一个 Q 扩展 QueryParams,则分别可以尝试直接放置 Specific 类型而不是。就像这样:

对于界面(在这里,最重要的是尽可能通用,因此,您可以选择两个名称,例如A,B或在您的情况下):

public interface EntityService<T, Q> {

T Save(T param);

void otherMethod(Q param);

}

对于装饰器(在这里,此时您可以选择类型,因此不必是“通用”):

public abstract class MyDecorator implements EntityService<BaseEntity, QueryParams>{

@Any
@Inject
@Delegate
EntityService<BaseEntity, QueryParams> delegate;
//If I understood well the problem, this just work and solve your problem, 
//or else... I'm sorry, you can give me more data so I could help you
//a little more

//Check here, I give a concrete type for T abstract generic type, in 
//your case BaseEntity
@Override
public BaseEntity save(BaseEntity entity) { 
    BaseEntity retSomething; //this is ilustrative
    super.save(entity);  //save your entity as you know how.
    return retSomething; //I really don't know why your save method has a 
                         //return statament, but, if you need it, check the 
                         //return type
}

@Override
public void otherMethod(QueryParams param) {
    // Check here, in the interface the name was Q, now here you only
    // can use as param an QueryParams type object

}

}


推荐