gpt4 book ai didi

java - 如何在 Spring 框架上使用抽象类?

转载 作者:行者123 更新时间:2023-11-30 05:38:48 25 4
gpt4 key购买 nike

我正在学习如何使用 Spring Boot 编写代码。然后,当我尝试编写使用抽象类的代码时,出现如下错误。

Description:

Parameter 0 of constructor in com.in28minutes.spring.practice.springmasterclasspractice.devicefactory.LaptopManufacturingProcess required a bean of type 'java.lang.String' that could not be found.

Action:
Consider defining a bean of type 'java.lang.String' in your configuration.

你们能给我建议如何解决这个错误吗?

Spring 启动:v2.1.4 java :10.0.2Maven:3.6.0

SpringMasterClassPracticeDeviceFactoryApplication

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringMasterClassPracticeDeviceFactoryApplication {

private static Logger LOGGER = LoggerFactory.getLogger(SpringMasterClassPracticeDeviceFactoryApplication.class);

public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication
.run(SpringMasterClassPracticeDeviceFactoryApplication.class, args);
ManufacturingImpl manufacturingImpl = applicationContext.getBean(ManufacturingImpl.class);

System.out.println(manufacturingImpl);
// manufacturingImpl.manifactureProduct("Laptop Process");

LOGGER.info("{}", manufacturingImpl);

}

}

ManufacturingImpl

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

@Component
public class ManufacturingImpl {

@Autowired
@Qualifier("laptop")
private GeneralManufacturingProcess generalManufacturingProcess;

public void manifactureProduct(String processName) {
System.out.println(generalManufacturingProcess);
generalManufacturingProcess.launchProcess();
}
}

GeneralManufacturingProcess

public abstract class GeneralManufacturingProcess {

private String processName;

public GeneralManufacturingProcess(String processName) {
this.processName = processName;
}

public String getProcessName() {
return processName;
}

public void launchProcess() {
if (processName != null && !processName.isEmpty()) {
assembleDevice();
testDevice();
packageDevice();
storeDevice();
} else {
System.out.println("No process name was specified");
}
}

protected abstract void assembleDevice();

protected abstract void testDevice();

protected abstract void packageDevice();

protected abstract void storeDevice();
}

LaptopManufacturingProcess

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

@Component
@Qualifier("laptop")
public class LaptopManufacturingProcess extends GeneralManufacturingProcess {

public LaptopManufacturingProcess(String processName) {
super(processName);
}

@Override
protected void assembleDevice() {
System.out.println("Assembled laptop: " + getProcessName());
}

@Override
protected void testDevice() {
System.out.println("Tested laptop: " + getProcessName());
}

@Override
protected void packageDevice() {
System.out.println("Packaged laptop: " + getProcessName());
}

@Override
protected void storeDevice() {
System.out.println("Stored laptop: " + getProcessName());
}
}

最佳答案

有多种方法可以解决这个问题。问题是,Spring 框架尝试使用单个构造函数创建 LaptopManufacturingProcess 的实例,该构造函数接受一个字符串。因此框架试图将 String 类型的 Bean Autowiring 到构造函数中,但这根本不起作用。基本上,您可以执行以下操作:

  • 创建一个无参数构造函数,并将硬编码字符串传递给父构造函数:
public LaptopManufacturingProcess() {
super("String");
}
  • 添加 @Value-Annotation 以从 PropertySource 读取字符串:
public LaptopManufacturingProcess(@Value("${property.key.here}") String processName) {
super(processName);
}
  • 创建一个 Factory Bean 以按需创建 GeneralManufacturingProcess 实例

关于java - 如何在 Spring 框架上使用抽象类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56108643/

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