簡體   English   中英

我想使用注解@ComponentScan,但出現錯誤“考慮定義類型的bean” SpringBoot 2.1.0.RELEASE

[英]I want to use anotation @ComponentScan but I get an error “Consider defining a bean of type” SpringBoot 2.1.0.RELEASE

我為框架實現了一個簡單的示例,這是想做一些更復雜的事情。

我有一個帶有Spring boot 2.1.0.Release的簡單maven項目。

結構如下:

package com.springBootLenr.services;

public interface HelloWorldService {
    public String sayHello();
}

包服務實現接口服務。

package com.springBootLenr.services;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("default, english")
public class HelloWorldEnglishImpl implements HelloWorldService{

   @Override
   public String sayHello() {
       return "Hello World in English";
   }
}

以上都是從控制器調用的。

package com.springBootLenr.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.springBootLenr.services.HelloWorldService;

@Controller
public class HelloWorldController {

    private HelloWorldService helloService;

    @Autowired
    public HelloWorldController(HelloWorldService helloService) {
        super();
        this.helloService = helloService;
    }

    public String greeting() {
        String greeting = helloService.sayHello();
        System.out.println(greeting);
        return greeting;
    }   
}

這是我的運行應用程序:

package com.springBootLenr.springBootLenr;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import com.springBootLenr.controllers.HelloWorldController;

@SpringBootApplication
@ComponentScan("com.springBootLenr")
public class SpringBootLenrApplication {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(SpringBootLenrApplication.class, args);
        HelloWorldController greeting = (HelloWorldController) ctx.getBean("helloWorldController");
        greeting.greeting();
    }
}

我原本希望使用"Hello world in English"類的東西"Hello world in English"但是卻收到此錯誤消息。

/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.0.RELEASE)

2018-11-08 19:35:35.511  INFO 11072 --- [           main] c.s.s.SpringBootLenrApplication          : Starting SpringBootLenrApplication on xxxxx with PID 11072 (D:\Datos\Proyectos\eclipse-workspace\springBootLenr\target\classes started by Yo_ in D:\Datos\Proyectos\eclipse-workspace\springBootLenr)
2018-11-08 19:35:35.517  INFO 11072 --- [           main] c.s.s.SpringBootLenrApplication          : No active profile set, falling back to default profiles: default
2018-11-08 19:35:36.161  WARN 11072 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloWorldController' defined in file [D:\Datos\Proyectos\eclipse-workspace\springBootLenr\target\classes\com\springBootLenr\controllers\HelloWorldController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.springBootLenr.services.HelloWorldService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2018-11-08 19:35:36.178  INFO 11072 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-11-08 19:35:36.478 ERROR 11072 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.springBootLenr.controllers.HelloWorldController required a bean of type 'com.springBootLenr.services.HelloWorldService' that could not be found.


Action:

Consider defining a bean of type 'com.springBootLenr.services.HelloWorldService' in your configuration.

問題是你的線:

@Profile("default, english")

激活“默認”或“英語”配置文件后,HelloWorldService的實現Bean僅對Spring“可見”-您在代碼中的任何地方都不會做(除非您沒有提到application.properties文件) :-P)。

您可以在SO上搜索有關默認情況下如何啟用配置文件的問題-例如您的示例中的“ default”-因為除非您這樣做,否則我認為Spring Boot不會為您這樣做。

順便說一句-我認為“適當”的語法是

@Profile({"default", "english"})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM