简体   繁体   中英

No Bean Found in Spring Boot application context

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@Configuration
@ConfigurationProperties("promotion")
public class PromotionConfig {
    private AppClientConfig appConfig;
}


@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AppClientConfig {
   private int readTImeout;
   private int writeTimeout;
}

When I am trying to do @Autowire PromotionConfig It is throwing an error that No Qualifying bean found for AppClientConfig.

I know this error is coming up because I dont have @Component in AppClientConfig.

How can i make it work without adding @Component in AppClientConfig. I want to keep it Plain POJO class.

My local.yml file

promotion:
  appConfig:
    readTImeout: 10
    writeTimeout: 10

Using Spring you have another way to do so: using Bean configuration:

@Bean
public AppClientConfig configureAppClientConfig(){
   return new AppClientConfig(); // You can use your own way to create the instance
}

Note: the above bean configuration should be implemented in a Spring configuration class.

And finally, you need to call using @Autowired :

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
@Configuration
@ConfigurationProperties("promotion")
public class PromotionConfig {
    @Autowired
    private AppClientConfig appConfig;
}

您可以在非 spring 类中静态加载 spring bean:

PromotionConfig PromotionConfig = ApplicationInitializer.getAppContext().getBean(PromotionConfig.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM