gpt4 book ai didi

java - 我想使用注释 @ComponentScan 但出现错误 "Consider defining a bean of type"Spring Boot 2.1.0.RELEASE

转载 作者:行者123 更新时间:2023-12-02 10:36:57 24 4
gpt4 key购买 nike

我实现了一个简单的框架示例,并且想做一些更复杂的事情。

我有一个简单的 maven 项目,其中包含 Spring boot 2.1.0.Release

结构如下:

package com.springBootLenr.services;

public interface HelloWorldService {
public String sayHello();
}

封装Service实现接口(interface)服务。

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";
}
}

以上所有内容都是从 Controller 调用的。

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.xml 文件)。您没有提到的属性文件:-p)。

您可以搜索有关如何默认启用配置文件的问题 - 就像您的示例中的“默认” - 因为我不认为 Spring Boot 会为您做这件事,除非您做到了。

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

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

关于java - 我想使用注释 @ComponentScan 但出现错误 "Consider defining a bean of type"Spring Boot 2.1.0.RELEASE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53218660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com