gpt4 book ai didi

java - 没有定义名为异常的 bean

转载 作者:行者123 更新时间:2023-11-30 08:42:41 25 4
gpt4 key购买 nike

包 com.mkyong.output;IOutputGenerator.java

public interface IOutputGenerator
{
public void generateOutput();
}

包 com.mkyong.output;输出助手.java

@Component
public class OutputHelper {

@Autowired
IOutputGenerator outputGenerator;

public void generateOutput() {
outputGenerator.generateOutput();
}

/*//DI via setter method
public void setOutputGenerator(IOutputGenerator outputGenerator) {
this.outputGenerator = outputGenerator;
}*/
}

包 com.mkyong.output.impl;

CsvOutputGenerator.java

@Component
public class CsvOutputGenerator implements IOutputGenerator {
public void generateOutput() {
System.out.println("This is Csv Output Generator");
}
}

SpringBeans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="com.mkyong" />

</beans>

我在线程 "main"org.springframework.beans.factory.NoSuchBeanDefinitionException 中得到这个异常异常:没有定义名为 'OutputHelper' 的 bean

即使我已将 OutputHelper 标记为组件。

最佳答案

我变了

OutputHelper output = (OutputHelper) context.getBean("OutputHelper");

OutputHelper output = (OutputHelper) context.getBean("outputHelper");

它奏效了。

关于java - 没有定义名为异常的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34490711/

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