Java 我想使用anotation@ComponentScan,但我得到一个错误";考虑定义一个类型的bean;SpringBoot 2.1.0.0版本

Java 我想使用anotation@ComponentScan,但我得到一个错误";考虑定义一个类型的bean;SpringBoot 2.1.0.0版本,java,spring-boot,Java,Spring Boot,我为这个框架实现了一个简单的示例,这是我想要做的更复杂的事情 我有一个简单的maven项目,其中包含springboot2.1.0.Release 结构如下所示: package com.springBootLenr.services; public interface HelloWorldService { public String sayHello(); } 包服务实现接口服务 package com.springBootLenr.services; import org.s

我为这个框架实现了一个简单的示例,这是我想要做的更复杂的事情

我有一个简单的
maven
项目,其中包含
springboot2.1.0.Release

结构如下所示:

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”的东西,但是,我收到了这个错误消息

/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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)

您可以搜索关于如何在默认情况下启用配置文件的问题,比如您的示例中的“default”,因为我认为SpringBoot不会为您做这件事,除非您做到了

顺便说一句,我认为“正确”的语法是

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

无论您在主方法中运行什么,都不受Spring上下文的影响,因此在上下文之外的方法中使用时,不会确认注册了注释的bean。您没有使用@service注释您的服务。因此,您必须使用@Service annotation而不是Componentchange将
HelloWorldEnglishImpl
上的
@Component
更改为
@Component(“helloService”)来注释实现类
并在
Controller
中声明无arg构造函数,我认为@ZhenyangHua夸大了这一点-但惯例是只在
main()
中调用
SpringApplication.run()
,并将所有代码放在一个实例中
run()
方法,Spring将在设置上下文后调用该方法…如果没有其他配置文件处于活动状态,
默认配置文件将自动激活。你关于语法的观点仍然有效。我怀疑这就是造成问题的原因。