I have two classes from custom library, that i can't change. Bass class have only constructor with custom param, that's not a bean. I want to pass param via child constructor, but i have no idea how to do that, so please help)
I tried this, but doesn't work. Idea underline param in Child constructor.
@Bean
public ChildClass childClass() {
return new ChildClass(new CustomParam(5));
}
Base class- can't use @Component
, that class from library
public abstract class BaseClass {
private CustomParam customParam;
protected BaseClass(CustomParam customParam) {
this.customParam = customParam;
}
public Integer getCustomParam() {
return customParam.getParamValue();
}
}
Child class. My own extension
@Component
public class ChildClass extends BaseClass {
//idea underline customParam "could not autowire"
public ChildClass(CustomParam customParam) {
super(customParam);
}
}
Param class- can't use @Component
, that class from library
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
need not to be annotated with @Component
annotation, still you can declare it as bean using@Bean
annotation
Config class
@Bean
public ChildClass childClass() {
return new ChildClass(customParam());
}
@Bean
public CustomParam customParam() {
return new CustomParam(5);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments