我有以下结构:
@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
通用参数。
MyDecorator
在beans.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>
任何线索?
答案 0 :(得分:1)
管理解决问题。我的问题是我在大多数端点/服务实现中直接使用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
。
答案 1 :(得分:0)
你这样做 - &gt;
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
}
}