作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习如何使用 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");
}
public LaptopManufacturingProcess(@Value("${property.key.here}") String processName) {
super(processName);
}
GeneralManufacturingProcess
实例关于java - 如何在 Spring 框架上使用抽象类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56108643/
我是一名优秀的程序员,十分优秀!