我有一个需要使用很少依赖项的自定义转换器。
由于转换器由 JPA 管理,我无法找出从另一个组件(如依赖注入器)传递依赖项的方法。有这样的方法吗?
@Converter
public class CompressingJsonConverter implements AttributeConverter<CompressedJson, Byte[]> {
private final Compressing compressing;
private final ObjectMapper objectMapper;
public CompressingJsonConverter() {
// I would like to inject those dependencies instead
compressing = new Compressing();
objectMapper = new ObjectMapper();
}
Run Code Online (Sandbox Code Playgroud)
rue*_*ste -1
尝试使用静态字段。要么你的 DI 框架支持静态注入(我知道 Guice 和 Salta 都支持),要么你必须在启动过程中手动完成。考虑在实用程序类中注册注入器(Guice、Salta)或实例(JavaEE/CDI),并在您需要的任何地方使用它。
归档时间: |
|
查看次数: |
821 次 |
最近记录: |