首页 > 解决方案 > 弹簧自动装配中的 ContextRefresher

问题描述

我有一个 java 应用程序,其中一个类具有 ContextRefresher 自动装配:

public abstract class AbstractConfigurationPersister implements IConfigurationPersister {

    @Autowired
    private ContextRefresher contextRefresher;

    @Override
    public void forceRefresh() {
        contextRefresher.refresh();
    }

}

这个自动装配的 bean 给我带来了日志中的问题,它说参数 1 有一个不满足的依赖关系。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration$DataSourceTransactionManagerConfiguration':

Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'application':

Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 
'ktc.tanalytics.prediction.PredictionBeanConfiguration':
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: 

Error creating bean with name 'configurationService' defined in ktc.tanalytics.commons.CommonsBeanConfiguration: 
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: 

Error creating bean with name 'zookeeperConfigurationPersister' defined in ktc.tanalytics.commons.CommonsBeanConfiguration: 
Initialization of bean failed; nested exception is 
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'contextRefresher' defined in class path resource [org/springframework/cloud/autoconfigure/RefreshAutoConfiguration.class]:
 Unsatisfied dependency expressed through method 'contextRefresher' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'refreshScope': 
Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'org.springframework.transaction.config.internalTransactionAdvisor' 
defined in class path resource [org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class]: 
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor]: 
Factory method 'transactionAdvisor' threw exception; nested exception is java.lang.NullPointerException

现在,我在 spring 中检查了 ContextRefresher 的代码(https://github.com/spring-cloud/spring-cloud-commons/blob/master/spring-cloud-context/src/main/java/org/springframework/ cloud/context/refresh/ContextRefresher.java),它接收两个参数。

我想 Spring 负责创建这个 ContextRefresher 对象,因为它基本上是@Autowired。但是,我不知道为什么日志中缺少参数 1

标签: javaspringspring-bootapache-zookeeper

解决方案


Factory method 'transactionAdvisor' threw exception; nested exception is java.lang.NullPointerException
  • 当我关注上面的嵌套异常时,可能transactionAdvisor 是一个工厂方法,它试图创建一些东西,并且在创建时,它正在对空对象执行一些操作。

  • 只需在您的代码中检查或调试方法: transactionAdvisor 。


推荐阅读