I want to initialize subclasses after a @PostConstruct of the super class
So I do somthing like that
// case 1: it's working but it's not simple to understand
public class A {
@PostConstruct
protected void init() {
System.out.println("A");
afterInit();
}
protected void afterInit() {}
}
public class B extends A {
@Override
protected void afterInit() {
System.out.println("B");
}
}
public class C extends B {
@Override
protected void afterInit() {
super.afterInit();
System.out.println("C");
}
}
So the init() method will print A, B, C in this order
It would be fine to have a @AfterPostconstruct annotation that would do the same but I don't find out
// case 2: dream code
public class A {
@PostConstruct
protected void init() {
System.out.println("A");
}
}
public class B extends A {
@AfterPostConstruct // pseudocode
protected void afterInitB() {
System.out.println("B");
}
}
public class C extends B {
@AfterPostConstruct // pseudocode
protected void afterInitC() {
System.out.println("C");
}
}
I try by overriding init() but it doesn't work (init() is not called by the container)
// case 3 : code that is not working but it would be better than case 1
public class A {
@PostConstruct
protected void init() {
System.out.println("A");
}
}
public class B extends A {
@Override
protected void init() {
super.init();
System.out.println("B");
}
}
public class C extends B {
@Override
protected void init() {
super.init();
System.out.println("C");
}
}
is there a better (simplier) way to initialize subclasses after @PostConstruct ?
Aucun commentaire:
Enregistrer un commentaire