gpt4 book ai didi

java - 我收到一个错误—— "No qualifying bean of type [hello.MessagePrinter] is defined"

转载 作者:搜寻专家 更新时间:2023-11-01 03:04:06 25 4
gpt4 key购买 nike

这是我从 spring 网站执行示例 spring 应用程序时遇到的错误。我试图找到解决方案但徒劳无功。请帮忙。我想它相当简单,但无法弄清楚。

文件应用程序.java

package hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
import org.springframework.context.support.AbstractApplicationContext;

@Configuration
@ComponentScan
public class Application {

@Bean
MessageService mockMessageService(){
return new MessageService() {
public String getMessage() {
return "Hello World!";
}
};
}

public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext();
((AbstractApplicationContext) context).refresh();
MessagePrinter printer = context.getBean(MessagePrinter.class);
printer.printMessage();

}

}

消息打印机.java

package hello;

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

@Component
public class MessagePrinter {
final private MessageService service;

@Autowired
public MessagePrinter(MessageService service){
this.service = service;
}

public void printMessage(){
System.out.println(this.service.getMessage());
}

}

消息服务.java

package hello;

public interface MessageService {
String getMessage();

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>gs-maven</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>hello.Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<!-- tag::joda[] -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.2</version>
</dependency>
<!-- end::joda[] -->
</dependencies>
</project>

错误:

Mar 2, 2015 10:50:01 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@c1b531: startup date [Mon Mar 02 10:50:01 IST 2015]; root of context hierarchy Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hello.MessagePrinter] is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:371) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968) at hello.Application.main(Application.java:23)

请让我知道我错过了什么

最佳答案

看这个样本

http://www.tutorialspoint.com/spring/spring_java_based_configuration.htm

ApplicationContext context = new AnnotationConfigApplicationContext(); 行采用入口点 bean 的参数。

所以你的代码应该是

ApplicationContext context = 
new AnnotationConfigApplicationContext(MessagePrinter.class);

如果你想注册一些 beans 那么你可以使用 register 或使用 scan 来扫描一个包

关于java - 我收到一个错误—— "No qualifying bean of type [hello.MessagePrinter] is defined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28803624/

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