2017-05-28 117 views
0

我創建一個控制器,服務和信息庫class.All一個春天啓動應用程序,這些定義如下:服務豆沒有得到自動裝配在春季啓動

MainClass

@SpringBootApplication 
public class MainClass extends SpringBootServletInitializer { 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(MainClass.class); 
    } 

    public static void main(String[] args) { 
     SpringApplication.run(MainClass.class, args); 
    } 

} 

控制器:

@RestController 
@EnableAutoConfiguration 
public class ExampleController { 

    public static final Logger logger = LoggerFactory.getLogger(ExampleController.class); 

    private CustomReportService customReportService; 

    @Autowired 
    public void setCustomReportService(CustomReportService customReportService){ 
     this.customReportService = customReportService; 
    } 

    @RequestMapping(value="/api/report/list", method= RequestMethod.GET) 
    public ResponseEntity<Collection<CustomReport>> listAllCustomReports(){ 
     return new ResponseEntity<>((Collection<CustomReport>) customReportService.listAllCustomReports(), HttpStatus.OK); 
    } 
} 

服務接口:

public interface CustomReportService { 
    Iterable<CustomReport> listAllCustomReports(); 
} 

服務默認地將Impl:

@Service 
public class CustomReportServiceImpl implements CustomReportService{ 

    @Autowired 
    private CustomReportRepository customReportRepository; 

    @Autowired 
    public void setCustomReportRepository(CustomReportRepository customReportRepository){ 
     this.customReportRepository = customReportRepository; 
    } 

    @Override 
    public Iterable<CustomReport> listAllCustomReports() { 
     return customReportRepository.findAll(); 
    } 

} 

庫:

public interface CustomReportRepository extends CrudRepository<CustomReport, Long> { 
} 

有關部署,我的應用程序無法啓動與FOLL錯誤:

2017-05-28 15:50:28.723 DEBUG 14708 --- [   main] o.s.b.d.LoggingFailure 
AnalysisReporter : Application failed to start due to an exception 

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.test.services.customreport.CustomReportService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:659) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.8.RELEASE.jar!/:4.3.8.RELEASE] 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE] 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE] 
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE] 

請告訴我,我要去哪裏錯了。

謝謝!

+0

您的類'CustomReportServiceImpl'不是由Spring管理的,或者Spring不可發現的。在你的配置類中檢查'@ ComponentScan'註解並相應地進行編輯,以便Spring容器可以找到它。 –

+0

讓我知道它是否修復它 –

+1

'@ EnableAutoConfiguration'僅適用於'@ Configuration'類。所以在你的情況下,它不會做任何事情,也可以通過使用'@ SpringBootApplication'來暗示。我懷疑你的'MainClass'f不在'com.test'中,而是在它的一些子包中。建議將bootstrapping類放在最高的包中,在你的例子'com.test'或任何實際的包中。由於'@ ComponentScan'被'@ SpringBootApplication'隱含,但是它從包開始,而MainClass不在其他包(因此是根包)中定義。 –

回答

1

從控制器中刪除@EnableAutoConfiguration,它已經在@SpringBootApplication。 您的MainClass應該位於根包中。 也嘗試添加@ComponentScan("your.root.package")您MainClass

1
  1. 確保你把你的主要春季啓動應用程序的入口 類MainClass在你的工作包的頂部,與所有其他 類下。
  2. 無需將@EnableAutoConfiguration添加到您的 控制器中,因爲@SpringBootApplication已包含該功能。
  3. CustomReportServiceImpl您正在混合字段&構造函數汽車 接線爲customReportRepository,只選擇一個。