Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有办法将依赖项传递给JPA转换器?_Java_Jpa - Fatal编程技术网

Java 有没有办法将依赖项传递给JPA转换器?

Java 有没有办法将依赖项传递给JPA转换器?,java,jpa,Java,Jpa,我有一个自定义转换器,它需要使用很少的依赖项 因为转换器是由JPA管理的,所以我无法找到一种从另一个组件(如依赖项注入器)传递依赖项的方法。有这样的方法吗 @Converter public class CompressingJsonConverter implements AttributeConverter<CompressedJson, Byte[]> { private final Compressing compressing; private final

我有一个自定义转换器,它需要使用很少的依赖项

因为转换器是由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();
    }
@转换器
公共类压缩JSONConverter实现AttributeConverter{
私有最终压缩;
私有最终ObjectMapper ObjectMapper;
公共压缩JSONConverter(){
//我希望注入这些依赖项
压缩=新压缩();
objectMapper=新的objectMapper();
}

< /代码> 尝试使用静态字段。您的DI框架支持静态注入(我知道Guice和萨尔塔DO),或者您必须在启动过程中手工操作。请考虑注册表注入器(Guice,萨尔塔)或实例(JavaEE/CDI)。在实用程序类中,并在任何需要的地方使用它。

您可以尝试将实用程序类作为单例使用。根据您的问题,我假设您有某种依赖注入系统

如果您可以确保在转换器类使用它之前,实用程序对象将被完全初始化,并且如果您的DI系统允许在注入后调用初始化方法(Spring允许),那么您可以有如下结果:

class Util {
    @Inject // or whatever you use for injection
    private Compressing compressing;
    @Inject // or whatever you use for injection
    private ObjectMapper objectMapper;
    // getters and setters omitted for brevity

    private static Util instance;
    public Util getInstance() {
        return instance;
    }

    // DI initialization method after attributes have been injected
    public void init() {
        instance = this;
    }
}
然后,您可以在转换器中执行以下操作:

@Converter
public class CompressingJsonConverter implements
        AttributeConverter<CompressedJson, Byte[]> {

    private Compressing compressing = null;
    private ObjectMapper objectMapper = null;

    private Compressing getCompressing() {
        if (compressing == null) {
            // load it first time from util, then caches it locally
            compressing = Util.getInstance().getCompressing();
        }
        return compressing;
    }
    // same for objectMapper
    ...
}

但一定要仔细检查它的工作情况,并用红色闪烁字体记录下来,因为它可能会在任何组件(DI、JPA、Java等)的每一个新版本中崩溃。

这取决于你的两个对象生活在哪个世界:

  • 如果两者都是由CID创建的,那么就不应该有任何问题,只要使用javax.inject就足够了
  • 如果一个是由不同的DI框架(比如GUICE)创建的,那么您需要编写一些粘合代码来连接它们
我在一个项目中使用的一种技术是创建一个类,如:

//创建一个新类
类桥{@Inject public static ObjectMapper ObjectMapper;}
//界面模块
requestStaticInjection(Bridge.class)
//内变频器
类MyConvertor{
私有对象映射器对象映射器;
公共MyConvertor(){
this.objectMapper=Bridge.objectMapper;
}
}
public CompressingJsonConverter() {
    // I would like to inject those dependencies instead
    compressing = Util.getInstance().getCompressing();
    objectMapper = Util.getInstance().getObjectMapper();
}