[英]Consider defining a bean of type 'com.example.demo.Commands$DefaultIO' in your configuration
I'm trying to run this code using Spring boot:我正在尝试使用 Spring 启动运行此代码:
public interface DefaultIO{
public String readText();
public void write( String text);
public float readVal();
public void write( float val);
public default void uploadFile(String exit, String outputPath) {
try {
PrintWriter writer = new PrintWriter(new FileWriter(outputPath));
String line;
while ( (line = readText()) != null) {
if(line.equals(exit))
break;
if(line.equals(""))
continue;
writer.println(line);
}
writer.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public default String[] downloadFile(String outputFileName) {
try {
Scanner scanner=new Scanner(new BufferedReader(new FileReader(outputFileName)));
String str="";
while (scanner.hasNext()){
str+=scanner.nextLine()+"split";
}
scanner.close();
return str.split("split");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return "doesnt work".split(" ");
}
void close();
}
DefaultIO dio;
public Commands(DefaultIO dio) {
this.dio=dio;
}
public abstract class Command{
protected String description;
public Command(String description) {
this.description=description;
}
public abstract void execute();
}
public class mainCommand extends Command{
public mainCommand() {
super("Welcome to the Anomaly Detection Server.\n" +
"Please choose an option:\n");
}
@Override
public void execute() {
CLI cli= new CLI(dio);
cli.commands.forEach(command -> dio.write( command.description));
int clientNumber = (int) dio.readVal();
cli.commands.get(clientNumber).execute();
}
}
and run this code from CLI class:并从 CLI class 运行此代码:
ArrayList<Command> commands;
DefaultIO dio;
Commands c;
public CLI(DefaultIO dio) {
this.dio=dio;
c=new Commands(dio);
commands=new ArrayList<>();
commands.add(c.new mainCommand());
commands.add(c.new uploadCommand());
commands.add(c.new algorithmCommand());
commands.add(c.new detectCommand());
commands.add(c.new displayCommand());
commands.add(c.new analyzeCommand());
commands.add(c.new exitCommand());
// implement
}
@GetMapping("/test")
public void start() {
// implement
commands.get(0).execute();
}
} }
this is my main:这是我的主要内容:
SpringApplication.run(DemoApplication.class, args);
}
private Scanner s;
@Override
public void run(String... args) throws Exception {
s = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
StandrtIO sio=new StandrtIO(s);
CLI cli=new CLI(sio);
cli.start();
s.close();
sio.close();
}
and i get this error:我得到这个错误:
Parameter 0 of constructor in com.example.demo.CLI required a bean of type 'com.example.demo.Commands$DefaultIO' that could not be found. com.example.demo.CLI 中构造函数的参数 0 需要找不到类型为“com.example.demo.Commands$DefaultIO”的 bean。
Action:行动:
Consider defining a bean of type 'com.example.demo.Commands$DefaultIO' in your configuration.考虑在您的配置中定义类型为“com.example.demo.Commands$DefaultIO”的 bean。
Spring is trying to inject a DeafultIO bean into the CLI constructor. Spring 正在尝试将 DeafultIO bean 注入 CLI 构造函数。
You need to implements the DeafultIO interface with a class and annotate that class with @Component
which will tell Spring it's a bean and needs to be initialized at start up.您需要使用 class 实现 DeafultIO 接口,并使用@Component
注释 class ,这将告诉 Spring 它是一个 bean,需要在启动时进行初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.