自动装入JPA转换器

时间:2016-04-26 05:24:36

标签: spring jpa spring-boot jackson spring-data-jpa

我在春季启动应用中使用自定义的ObjectMapper。我还将JPA转换器用于多个字段,这些字段在DB中存储为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做到这一点?

1 个答案:

答案 0 :(得分:17)

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

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

    private static ObjectMapper objectMapper; 

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