简体   繁体   English

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

[英]Java CDI: Decorator with multiple generic params

I have the following structure: 我有以下结构:

@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) { ... }

} 

This is the EntityService interface declaration: 这是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);

}

Unfortunately, the decorator save method never get called when it should, although no errors are shown ... The only way I got it working was like this: 不幸的是,装饰器保存方法永远不会被调用,虽然没有显示错误...我得到它的唯一方法是这样:

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

Without the Q extends QueryParams generic param. 没有Q extends QueryParams通用参数。

The MyDecorator is declared inside beans.xml . 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>

Any clues? 有线索吗?

Managed to solve the issue. 管理解决问题。 My problem was that I was using QueryParams directly in most of the endpoints/services implementations, for instance: 我的问题是我在大多数端点/服务实现中直接使用QueryParams ,例如:

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

In fact, QueryParams don't actually extends QueryParams , it is the class itself! 实际上, QueryParams实际上并没有extends QueryParams ,它本身就是类! That's why the PersonService wasn't triggering MyDecorator at all! 这就是PersonService根本没有触发MyDecorator的原因!

Being so I created an interface called IQueryParams and used that instead, like that: 因此,我创建了一个名为IQueryParams的接口,并使用它,就像那样:

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

Now PersonService save method does trigger the MyDecorator . 现在PersonService保存方法确实触发了MyDecorator

You are doing this -> 你这样做 - >

EntityService<T extends BaseEntity, Q extends QueryParams>

If that interface take two Object, one T extends BaseEntity, other Q extends QueryParams, Respectively, you can try directly put the Specific types instead of. 如果该接口有两个Object,则一个T扩展BaseEntity,其他Q扩展QueryParams,分别可以尝试直接放入Specific类型而不是。 Just like this: 像这样:

To the interface (Here, the most important is to be as generic as posible, so, you pick two names, like A,B, or in your case): 到界面(这里,最重要的是像posible一样通用,所以,你选择两个名字,比如A,B,或者在你的情况下):

public interface EntityService<T, Q> {

T Save(T param);

void otherMethod(Q param);

} }

To the decorator (Here, at this point, you can choose the types, so is not necesary to be "generic"): 对于装饰者(在这里,你可以选择类型,因此不必是“通用”):

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