java - 如果当前类是spring bean,如何使用抽象类中的参数调用 super 构造函数?

标签 java spring spring-boot

我有两个来自自定义库的类,我无法更改。 Bass 类只有带有自定义参数的构造函数,这不是一个 bean。我想通过子构造函数传递参数,但我不知道该怎么做,所以请帮忙)

我尝试过这个,但不起作用。想法子构造函数中的下划线参数。

@Bean
public ChildClass childClass() {
    return new ChildClass(new CustomParam(5));
}

基类 - 不能使用@Component,该类来自库

public abstract class BaseClass {

private CustomParam customParam;

protected BaseClass(CustomParam customParam) {
    this.customParam = customParam;
}

public Integer getCustomParam() {
    return customParam.getParamValue();
}
}

child 类。我自己的扩展

@Component
public class ChildClass extends BaseClass {

//idea underline customParam "could not autowire"
public ChildClass(CustomParam customParam) {
    super(customParam);
}
}

Param 类 - 不能使用 @Component,该类来自库

public class CustomParam {
private Integer paramValue;

public CustomParam(Integer paramValue) {
    this.paramValue = paramValue;
}

public Integer getParamValue() {
    return paramValue;
}

public void setParamValue(Integer paramValue) {
    this.paramValue = paramValue;
}
}

最佳答案

CustomParam不需要使用@Component注解进行注解,仍然可以使用@Bean注解将其声明为bean

配置类

@Bean
public ChildClass childClass() {
    return new ChildClass(customParam());
 }

  @Bean
public CustomParam customParam() {
    return new CustomParam(5);
 }

关于java - 如果当前类是spring bean,如何使用抽象类中的参数调用 super 构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57217092/

相关文章:

java - 将 Spring Boot 1.3.2 升级到 1.4.1 后,Hibernate hbm2ddl (ddl-auto) 失败

java - 如何在 Eclipse 中用新内容替换搜索到的行?

java - 根据任务结果取消计划的固定费率任务

java - 将 SSL 添加到 Spring Boot 应用程序

spring - 为什么 Spring Boot 在 Mac OSX Mavericks (10.9.2) 上启动非常慢?

javascript - 单击 html 链接时禁用 spring boot 请求检查

java - 当我创建方法时,我的 Java 代码显示编译器错误

用于限制返回对象的可用方法的 Java 设计模式

java - java中的扫描仪问题

spring - 如何使用 spring-security-oauth2 支持消费应用程序可配置身份验证提供程序