Spring启动两个对象映射器bean

时间:2017-10-03 15:55:41

标签: spring spring-boot

我想在Spring启动项目中创建两个Hibernate感知对象映射器bean。

一个bean强制延迟加载,另一个强制延迟加载设置为false。我的bean定义如下:

我创建了一个HibernateAwareObjectMapper

public class HibernateAwareObjectMapper extends ObjectMapper {
}

@Configuration
public class CrewuiserCorebeans extends WebMvcConfigurerAdapter {

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

@Bean
public ObjectMapper defaultObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    Hibernate5Module hibernate5Module = new Hibernate5Module();
    hibernate5Module.configure(Hibernate5Module.Feature.FORCE_LAZY_LOADING, true);
    objectMapper.registerModule(hibernate5Module);
    return objectMapper;
}

@Bean
public HibernateAwareObjectMapper hibernateAwareObjectMapper() {
    HibernateAwareObjectMapper objectMapper = new HibernateAwareObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    Hibernate5Module hibernate5Module = new Hibernate5Module();
    hibernate5Module.configure(Hibernate5Module.Feature.FORCE_LAZY_LOADING, false);
    objectMapper.registerModule(hibernate5Module);
    return objectMapper;
}


@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
    argumentResolvers.add(new UserContextResolver());
}

}

当我运行应用程序时,我收到以下错误:

Parameter 0 of method mappingJackson2HttpMessageConverter in org.springframework.boot.autoconfigure.web.JacksonHttpMessageConvertersConfiguration$MappingJackson2HttpMessageConverterConfiguration required a single bean, but 2 were found:
- defaultObjectMapper: defined by method 'defaultObjectMapper' in class path resource [com/crewuiser/core/configuration/CrewuiserCorebeans.class]
- hibernateAwareObjectMapper: defined by method 'hibernateAwareObjectMapper' in class path resource [com/crewuiser/core/configuration/CrewuiserCorebeans.class]

org.springframework.boot.autoconfigure.web.JacksonHttpMessageConvertersConfiguration在jar中,因此我无法添加@Qualifier注释。

请帮助,如果我在这里遗漏任何东西。我怎么能让它起作用?

2 个答案:

答案 0 :(得分:3)

我找到了解决方案。

我将@Primary添加到其中一个bean中,并在非主要bean需要@Qualifier的其他类中使用@Autowired

@Bean
@Primary
public ObjectMapper defaultObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return objectMapper;
}

并在我的服务类中。

@Autowired
public OrganisationService(OrganisationValidator organisationValidator, OrganisationAuthority organisationAuthority,
                           OrganisationHelper organisationHelper, OrganisationRepository organisationRepository,
                           @Qualifier("hibernateAwareObjectMapper") ObjectMapper hibernateAwareObjectMapper) {}

答案 1 :(得分:0)

你可以给bean命名如下:

@Bean("bean1")
public ObjectMapper defaultObjectMapper() {
.......
}

@Bean("bean2")
public HibernateAwareObjectMapper hibernateAwareObjectMapper() {
.....    
}

以及要自动装配的地方,对bean1使用@Qualifier("bean1"),对bean2使用@Qualifier("bean2")