2017-12-15 8 views
0

これはコンフィグレーションクラスのBean定義です。コンフィグレーションクラスで初期化されても、Spring Beanはまだヌルです

@Bean(name = "applicationPropertiesDataService") 
public com.ing.app.data.ApplicationPropertiesDataService 
applicationPropertiesDataService() { 
    return new com.ing.app.data.ApplicationPropertiesDataService(); 
} 

これは私が上記のBeanを使用しているクラスの私のbean定義です。

@Bean(name = "appRestTemplate") 
public AppRestTemplate appRestTemplate() { 
    return new AppRestTemplate(); 
} 

これはAppRestTemplateクラスです。 BeanがAppRestTemplate豆の前にインスタンス化されるにもかかわらず、私はヌルとしてautowired「applicationPropertiesDataService」Beanを取得しています、私は、なぜ把握することができません

import org.springframework.web.client.RestTemplate; 

import com.ing.app.data.ApplicationPropertiesDataService; 
import com.ing.app.interceptor.LoggingClientHttpRequestInterceptor; 

public class AppRestTemplate extends RestTemplate { 

@Autowired 
private ApplicationPropertiesDataService applicationPropertiesDataService; 

public AppRestTemplate() { 

    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); 
    requestFactory.setOutputStreaming(false); 
    BufferingClientHttpRequestFactory bufferingClientHttpRequestFactory = 
     new BufferingClientHttpRequestFactory(requestFactory); 
    this.setRequestFactory(bufferingClientHttpRequestFactory); 

    if (isLoggingEnabled()) { 

    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); 
     interceptors.add(new LoggingClientHttpRequestInterceptor()); 
     this.setInterceptors(interceptors); 
    } 
} 
} 

private boolean isLoggingEnabled() { 
    boolean isLoggingEnabled = false; 
    Optional<ApplicationProperties> applicationProperties = applicationPropertiesDataService.findByCategoryAndName(
     Constant.APPLICATION_PROPERTIES_CATEGORY_AUDIT_LOGGING, Constant.APPLICATION_PROPERTIES_AUDIT_LOGGING); 

    if (applicationProperties.isPresent()) { 
     isLoggingEnabled = Constant.CONSTANT_Y.equalsIgnoreCase(applicationProperties.get().getValue()); 
    } 
    return isLoggingEnabled; 
} 

(Iは、コンフィギュレーションクラスにデバッグポイントを置くことによって、それをチェックします) autowired applicationPropertiesDataService bean nullです。どんな助けもありがとう。ありがとう。

+0

:あなたはこれを行うことができます。 ApplicationPropertiesDataServiceのクラス定義も表示する – KayV

答えて

1

new AppRestTemplate();を手動で呼び出すと、CDI(Context and Dependency Injection)をバイパスします。 Autowiredを取得するには、SpringがBeanを作成する必要があります。

多くの解決策があります。あなたのエラーログを表示してください

@Bean(name = "appRestTemplate") 
public AppRestTemplate appRestTemplate(ApplicationPropertiesDataService applicationPropertiesDataService) { 
    return new AppRestTemplate(applicationPropertiesDataService); 
} 

public class AppRestTemplate extends RestTemplate { 

    private final ApplicationPropertiesDataService applicationPropertiesDataService; 

    @Autowired 
    public AppRestTemplate(ApplicationPropertiesDataService applicationPropertiesDataService) { 
     this.applicationPropertiesDataService = applicationPropertiesDataService; 
    } 
} 
関連する問題