Java 如何在spring框架上使用抽象类?

Java 如何在spring框架上使用抽象类?,java,maven,spring-boot,Java,Maven,Spring Boot,我是学习如何使用Spring Boot编写代码的人。然后,当我试图编写一个使用抽象类的代码时,我得到一个错误,如下所示 Description: Parameter 0 of constructor in com.in28minutes.spring.practice.springmasterclasspractice.devicefactory.LaptopManufacturingProcess required a bean of type 'java.lang.String' that

我是学习如何使用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.
你们能给我一个如何解决这个错误的建议吗

弹簧靴:v2.1.4 Java:10.0.2 Maven:3.6.0

SpringMasterClassPracticeDeviceFactoryApplication
class

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);

    }

}

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();
    }
}
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();
}
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());
    }
}

ManufacturingImpl
class

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);

    }

}

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();
    }
}
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();
}
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());
    }
}

GeneralManufacturingProcess
class

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);

    }

}

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();
    }
}
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();
}
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());
    }
}

LaptoManufacturingProcess
class

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);

    }

}

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();
    }
}
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();
}
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框架试图使用单个构造函数创建LaptoManufacturingProcess的实例,该构造函数接受字符串。因此,框架试图将String类型的Bean自动连接到构造函数中,但这根本不起作用。 基本上,您可以执行以下操作:

  • 创建无参数构造函数,并将硬编码字符串传递给父构造函数:
  • 添加@Value注释以从PropertySource读取字符串:
  • 创建一个工厂Bean,根据需要创建
    GeneralManufacturingProcess
    的实例

异常会准确地告诉您错误所在,它与抽象超类无关。。。您的
LaptoManufacturingProcess
具有需要
字符串的构造函数。在类型为
String
的配置中没有其他bean。你可能想要的是,放弃
LaptoManufacturingProcess
上的注释,而是在
SpringMasterClassPracticeDeviceFactoryApplication
中创建一个名为
laptop
@Bean
方法,该方法使用适当的
字符串调用构造函数。您想用什么
processName
laptop
bean?似乎您正试图通过构造函数参数化
LaptopManufacturingProcess
。大多数春豆都是单粒的。将
processName
参数移动到
launchProcess
调用中,并将其作为局部变量全程跟踪。