弹簧Predicate
的{{1}}是静态还是非静止的?
默认实现类似如下:
Specification
但这也可以重构为:
public static Specification<Customer> byCustomerName(String name) {
return new Specification<Customer>() {
@Override
public Predicate toPredicate(Root<BasePricingCache> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.equal(root.get(Customer_.name), name);
}
};
}
应该优先考虑什么? private static final CUSTOMER_SPEC = return new Specification<Customer>() {
@Override
public Predicate toPredicate(Root<BasePricingCache> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.equal(root.get(Customer_.name), name);
}
};
public static Specification<Customer> byCustomerName(String name) {
return CUSTOMER_SPEC;
}
线程是否安全且可以这样使用?
答案 0 :(得分:1)
You're refactored code wouldn't even compile. There's a superfluous return, CUSTOMER_SPEC
doesn't have a type, and you're referring to a name
variable that doesn't exist in the scope.
I think you're overthinking the issue here and are in the process of micro-optimizing. Keeping the Specification
in a static factory method should be fine.
答案 1 :(得分:0)
线程安全不会成为实施的关注点。你根本不保留任何状态,每个参数都由调用者传递。
它可能是微优化的,但是当你的规范发生很多实例化时,你的重构会很有用。这可能会经常触发GC,从而导致响应速度明显滞后。
这种微观优化的缺点可能是你可能最终需要相当大的内存,因为实例不能用于GC。
首先通过收集数据(分析堆,......)来备份您的决定,并测试您的解决方案对应用程序的影响。