我有两个来自自定义库的类,我无法更改。 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/