自动装入JPA转换器

fal*_*con 9 spring jpa jackson spring-data-jpa spring-boot

我在我的春季启动应用程序中使用自定义的ObjectMapper.我还将JPA转换器用于多个字段,这些字段在DB中存储为JSON字符串.我不知道如何将我的自定义对象映射器自动装入我的转换器.

@Convert(converter=AddressConverter.class)
private Address address;
Run Code Online (Sandbox Code Playgroud)

我的AddressConverter是

class AddressConverter implements AttributeConverter<Address, String> {

        @Autowire
        ObjectMapper objectMapper; //How to do this?
        .....
        .....
   }
Run Code Online (Sandbox Code Playgroud)

怎么自动ObjectMapper装入AddressConverter?有没有办法用Spring AOP做到这一点?

xie*_*rui 18

也许你可以通过将其更改为静态属性来实现,如下所示:

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

    private static ObjectMapper objectMapper; 

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper){
        AddressConverter.objectMapper = objectMapper;
    }
    .....
    .....
}
Run Code Online (Sandbox Code Playgroud)

}