升级spring boot:由于循环依赖关系,rest模板bean创建失败

hfwmuf9z  于 2021-07-16  发布在  Java
关注(0)|答案(0)|浏览(246)

我正在将SpringBoot应用程序从1.5.6.release更新到2.3.5,但在生成项目时出现异常。下面是我的错误类:

@Configuration
@EnableBatchProcessing
@Import({BatchScheduler.class})
public class MyLogBatchConfiguration {

  private static final Logger log = LoggerFactory.getLogger(VToolLogBatchConfiguration.class);

  @Autowired private SimpleJobLauncher jobLauncher;

  @Autowired
  @Qualifier(value = "dataSource1")
  public DataSource mainDataSource;

  @Autowired
  @Qualifier(value = "dataSource2")
  public DataSource tempDataSource;

  @Autowired public StepBuilderFactory stepBuilderFactory;

  @Autowired public JobBuilderFactory jobBuilderFactory;

  @Autowired private RestTemplate myRestTemplate;

  @Qualifier(value = "createLocalSessionFactory")
  @Autowired
  private SessionFactory createLocalSessionFactory;

  @Qualifier(value = "createSessionFactory")
  @Autowired
  private SessionFactory createSessionFactory;

  @Bean
  RestTemplate restTemplate(RestTemplateBuilder builder) {
    RestTemplate restTemplate = new RestTemplate();
    List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
    for (HttpMessageConverter<?> converter : converters) {
      if (converter instanceof MappingJackson2HttpMessageConverter) {
        MappingJackson2HttpMessageConverter jsonConverter =
            (MappingJackson2HttpMessageConverter) converter;
        jsonConverter.setObjectMapper(new ObjectMapper());
        jsonConverter.setSupportedMediaTypes(
            ImmutableList.of(
                new MediaType(
                    "application", "json", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET),
                new MediaType(
                    "text", "javascript", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET)));
      }
    }
    return restTemplate;
  }

  @Bean
  public LocalSessionFactoryBean createLocalSessionFactory(
      @Qualifier("dataSource1") DataSource dataSource) {
    LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
    factory.setDataSource(dataSource);
    factory.setPackagesToScan(
        new String[] {
          "X",
          "Y",
          "Z"
        });
    factory.setHibernateProperties(hibernateProperties());
    return factory;
  }

  @Bean
  public LocalSessionFactoryBean createSessionFactory(
      @Qualifier("dataSource2") DataSource dataSource) {
    LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
    factory.setDataSource(dataSource);
    factory.setPackagesToScan(
        new String[] {
          "X",
          "Y",
          "Z"
        });
    factory.setHibernateProperties(hibernateProperties());
    return factory;
  }

我收到以下错误:
org.springframework.beans.factory.unsatifieddependencyException:创建名为“mylogbatchconfiguration”的bean时出错:通过字段“myrestemplate”表示的未满足的依赖关系;嵌套异常为org.springframework.beans.factory.unsatifieddependencyException:创建名为“batchconfiguration”的bean时出错:通过字段“myrestemplate”表示的未满足的依赖关系;嵌套异常为org.springframework.beans.factory.beancreationexception:创建名为“restemplate”的bean时出错,该bean在类路径资源[com//configuration/mylogbatchconfiguration.class]中定义:通过工厂方法示例化bean失败;嵌套异常为org.springframework.beans.beaninstantiationexception:未能示例化[org.springframework.web.client.resttemplate]:涉及包含bean的循环引用“mylogbatchconfiguration”-请考虑将工厂方法声明为静态,以独立于其包含示例。工厂方法“resttemplate”引发异常;嵌套的异常是org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredFeldElement.inject(autowiredannotationbeanpostprocessor)的java.lang.nullpointerexception。java:643)位于org.springframework.beans.factory.annotation.injectionmetadata.injector(injectionmetadata)。java:130)在org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocessproperties(autowiredannotationbeanpostprocessor)。java:399)位于org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory)。java:1420)在org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory)。java:593)位于org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory)。java:516)在org.springframework.beans.factory.support.abstractbeanfactory.lambda$dogetbean$0(abstractbeanfactory)。java:324)]在sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl。java:43)在java.lang.reflect.method.invoke(方法。java:498)在org.springframework.boot.loader.mainmethodrunner.run(mainmethodrunner。java:49)在org.springframework.boot.loader.launcher.launcher(launcher。java:107)在org.springframework.boot.loader.launcher.launch(launcher。java:58)在org.springframework.boot.loader.jarlauncher.main(jarlauncher。java:88)原因:org.springframework.beans.factory.unsatifieddependencyException:创建名为“batchconfiguration”的bean时出错:未满足通过字段“myrestemplate”表示的依赖关系;嵌套异常为org.springframework.beans.factory.beancreationexception:创建名为“restemplate”的bean时出错,该bean在类路径资源[com//configuration/mylogbatchconfiguration.class]中定义:通过工厂方法示例化bean失败;嵌套异常为org.springframework.beans.beaninstantiationexception:未能示例化[org.springframework.web.client.resttemplate]:涉及包含bean的循环引用“mylogbatchconfiguration”-请考虑将工厂方法声明为静态,以独立于其包含示例。工厂方法“resttemplate”引发异常;嵌套的异常是org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredFeldElement.inject(autowiredannotationbeanpostprocessor)的java.lang.nullpointerexception。java:643)位于org.springframework.beans.factory.annotation.injectionmetadata.injector(injectionmetadata)。java:130)在org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocessproperties(autowiredannotationbeanpostprocessor)。java:399)位于org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory)。java:1420)在org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory)。java:322)在org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory。java:202)在org.springframework.beans.factory.config.dependencydescriptor.resolvecandidate(dependencydescriptor。java:276)在org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory)。java:1307)位于org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory)。java:1227)在org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredFeldElement.inject(autowiredannotationbeanpostprocessor)。java:640) ... 28由于以下原因而忽略的公共帧:org.springframework.beans.factory.beancreationexception:创建名为“restemplate”的bean时出错,该bean在类路径资源[com/*/configuration/mylogbatchconfiguration.class]中定义:通过工厂方法示例化bean失败;嵌套异常为org.springframework.beans.beaninstantiationexception:未能示例化[org.springframework.web.client.resttemplate]:涉及包含bean的循环引用“mylogbatchconfiguration”-请考虑将工厂方法声明为静态,以独立于其包含示例。工厂方法“resttemplate”引发异常;嵌套的异常是java.lang.nullpointerexception,位于org.springframework.beans.factory.support.constructorresolver.instantiate(constructorresolver)。java:655)在org.springframework.beans.factory.support.constructorresolver.instantialEusingFactoryMethod(constructorresolver。java:635)在org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.instanceUsingFactoryMethod(abstractautowirecapablebeanfactory)。java:1336)位于org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory)。java:1176)在org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory)。java:556)位于org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory)。java:516)在org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory)。java:1307)位于org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory)。java:1227)在org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredFeldElement.inject(autowiredannotationbeanpostprocessor)。java:640) ... 50由于以下原因而忽略的公共框架:org.springframework.beans.beaninstantiationexception:未能示例化[org.springframework.web.client.resttemplate]:涉及包含bean的循环引用“mylogbatchconfiguration”-考虑将工厂方法声明为静态,以独立于其包含示例。工厂方法“resttemplate”引发异常;嵌套的异常是java.lang.nullpointerexception,位于org.springframework.beans.factory.support.simpleinstationstrategy.instantiate(simpleinstationstrategy)。java:185)在org.springframework.beans.factory.support.constructorresolver.instantiate(constructorresolver。java:650) ... 63由于java.lang.nullpointerexception:null在org.springframework.util.mimetype.(mimetype。java:143)在org.springframework.http.mediatype。java:433)
我省略了一些堆栈之间的跟踪,以保持重要的行在1.5.6.release版本中正常工作。我在这里看不到任何循环依赖关系。resttemplate函数也不依赖于mylogbatchconfiguration。我还试图改变顺序,将myrestemplatebean放在createsessionfactory bean之后,但仍然只有restemplate调用失败,而不是other。似乎other创建成功了。为什么要升级?任何帮助都将不胜感激。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题