So I have a class structure like so:
public class A {
private String foo;
protected A() {};
protected A(String foo) {
this.foo = foo;
}
public String getFoo() {
return foo;
}
}
public class B extends A {
public B() {};
public String getSomething(String test) {
return super.getFoo() + test;
}
}
public class C extends A {
public C() {};
public String getSomethingElse(String test) {
return super.getFoo() + test;
}
}
Here are my java configuration files:
@Configuration
public class ConfigA {
@Bean
public A a() {
return new A("FinalString");
}
}
@Configuration
public class ConfigChildren {
@Bean
public B b() {
return new B();
}
@Bean
public C c() {
return new C();
}
}
Obviously the problem is that the super
value is the child classes are null so doing super.getFoo
will be null even though the configuration is initialized.
I'm wondering how to connect these two in the configuration. I know in spring XML there's a way to do it with the parent
field but I don't know how to do that in java config.
I tried playing around with singletons but I don't think that's really applicable or good practice here. Is there a way to initialize A
for the child classes without passing the string variable into every child and having it do super(foo)
. Or is that the only way to do it?