自动装配到 JPA 转换器

Autowiring into JPA converters

我在 spring 引导应用程序中使用自定义的 ObjectMapper。我还将 JPA 转换器用于多个字段,这些字段在数据库中存储为 JSON 字符串。我不确定如何将我的自定义对象映射器自动连接到我的转换器中。

@Convert(converter=AddressConverter.class)
private Address address;

我的 AddressConverter 是

class AddressConverter implements AttributeConverter<Address, String> {

        @Autowire
        ObjectMapper objectMapper; //How to do this?
        .....
        .....
   }

如何将 ObjectMapper 自动连接到 AddressConverter?有没有办法用 Spring AOP 做到这一点?

也许您可以将其更改为 static 属性,如下所示:

@Component
class AddressConverter implements AttributeConverter<Address, String> {

    private static ObjectMapper objectMapper; 

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper){
        AddressConverter.objectMapper = objectMapper;
    }
    .....
    .....
}