繁体   English   中英

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

[英]Java CDI: Decorator with multiple generic params

我有以下结构:

@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);

}

不幸的是,装饰器保存方法永远不会被调用,虽然没有显示错误...我得到它的唯一方法是这样:

@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>

有线索吗?

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

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

实际上, QueryParams实际上并没有extends QueryParams ,它本身就是类! 这就是PersonService根本没有触发MyDecorator的原因!

因此,我创建了一个名为IQueryParams的接口,并使用它,就像那样:

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

现在PersonService保存方法确实触发了MyDecorator

你这样做 - >

EntityService<T extends BaseEntity, Q extends QueryParams>

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

到界面(这里,最重要的是像posible一样通用,所以,你选择两个名字,比如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

}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM