Spring Bean未自动装配

时间:2017-02-02 09:22:46

标签: java spring inversion-of-control

我在从DefaultConfigurationService类扩展的Spring bean abstract初始化时遇到问题。我完全陷入困境。

类层次结构如下:

public interface ConfigurationService {
    TaxPayer getTaxPayer();
} 

这个类被认为对需要初始化的服务很有用:

public abstract class BaseInitializationAwareService {

    private boolean initialized = false;

    public abstract void initialize();

    protected void checkInitialization() {
        if (!initialized) {
            initialize();
        }
    }

    protected void setInitialized() {
        this.initialized = true;
    }
}

此类充当配置服务的基类。

public abstract class BaseConfigurationService extends BaseInitializationAwareService implements ConfigurationService {


}

使用这个充当配置服务的bean是一个问题:

    public class DefaultConfigurationService extends BaseConfigurationService {

        private TaxPayerService taxPayerService;

        @Autowired
        public void setTaxPayerService(TaxPayerService taxPayerService) {
            Assert.notNull(taxPayerService);
            this.taxPayerService = taxPayerService;
        }

public void initialize() {
        Optional<TaxPayer> dbtaxPayer = taxPayerService.getActiveTaxPayer();
        if (!dbtaxPayer.isPresent()) {
            throw new IllegalStateException("Tax payer setting not found!");
        }
        this.taxPayer = dbtaxPayer.get();
        setInitialized();
    }

    // the rest omitted...
    }

当我创建DefaultConfigurationService bean时:

@Bean
    public BaseConfigurationService configurationService() {
        DefaultConfigurationService configurationService = new DefaultConfigurationService();
        configurationService.initialize();
        return configurationService;
    }

然后taxPayerService中的DefaultConfigurationService为空 - 似乎没有自动装配。

可以与DefaultConfigurationService从抽象类扩展的事实联系起来吗?

TaxPayer服务bean:

@Bean
    public TaxPayerService taxPayerService() {
        DatabaseTaxPayerService taxPayer = new DatabaseTaxPayerService();
        return taxPayer;
    }

这个bean可能从未初始化 ...

这是一个例外:

  

org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名为'webSecurityConfig.ApiSecurity'的bean时出错:   通过方法表达不满意的依赖性   'setContentNegotationStrategy'参数0;嵌套异常是   org.springframework.beans.factory.UnsatisfiedDependencyException:   使用名称创建bean时出错   'org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration':   通过方法'setConfigurers'表示不满意的依赖   参数0;嵌套异常是   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名为'passwordRecoverController'的bean时出错:不满意   通过方法'setUserService'参数0表示的依赖关系;   嵌套异常是   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名为'defaultUserService'的bean时出错:不满意   通过方法'setNotificationService'参数表示的依赖关系   0;嵌套异常是   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名为'notificationService'的bean时出错:不满意   通过方法'setConfigurationService'表示的依赖关系   参数0;嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   在类路径中定义名为'configurationService'的bean   resource [com.example.config / AppConfig.class]:Bean实例化via   工厂方法失败;嵌套异常是   org.springframework.beans.BeanInstantiationException:失败   实例化[com.example.services.BaseConfigurationService]:Factory   方法'configurationService'抛出异常;嵌套异常是   显示java.lang.NullPointerException

例如,需要BaseConfigurationService的bean:

    public class EmailNotificationService extends BaseService implements NotificationService {

        private BaseConfigurationService configurationService;

@Autowired
    public void setConfigurationService(BaseConfigurationService configurationService) {
        Assert.notNull(configurationService);
        this.configurationService = configurationService;
    }

    // the rest omitted... 
    }

#update 1

Bean初始化示例,内部依赖于另一个bean:

@Bean
    public TransactionDataService transactionDataService() {
        return new DefaultTransactionDataService();
    }

DefaultTransactionDataService

public class DefaultTransactionDataService implements TransactionDataService {
    private PrivateKeyService privateKeyService;

    @Autowired
    public void setPrivateKeyService(PrivateKeyService privateKeyService) {
        Assert.notNull(privateKeyService);
        this.privateKeyService = privateKeyService;
    }
}

和bean依赖

@Bean
    public PrivateKeyService privateKeyService() {
        return new DefaultPrivateKeyAwareService();
    }

有效

1 个答案:

答案 0 :(得分:0)

它与您对Spring的指令有关,如何初始化bean:

  

@Bean用于显式声明单个bean,而不是让Spring像上面那样自动执行它。它将bean的声明与类定义分离,并允许您根据自己的选择创建和配置bean。

因此,当您手动创建类的实例时,您还必须手动设置所有类字段。

可以这样做:

@Bean
public BaseConfigurationService configurationService() {
    DefaultConfigurationService configurationService = new DefaultConfigurationService();
    configurationService.setTaxPayerService(taxPayerService());
    configurationService.initialize();
    return configurationService;
}

或者将依赖项直接移动到构造函数,因此DefaultConfigurationService的依赖关系会更加明显:

@Bean
public BaseConfigurationService configurationService() {
    DefaultConfigurationService configurationService = new DefaultConfigurationService(taxPayerService());
    configurationService.initialize();
    return configurationService;
}

Good description