I was reading this post when I thought about the possibility of substituting any @Bean
(Spring DI) or @Produces
(CDI) with a simple @PostConstructor
as in the following CDI example:
Replace:
public class MyClassFactory {
@Produces
@RequestScoped
public MyClass createMyClass() {
MyClass myClass = new MyClass();
myClass.setA(1);
return myClass;
}
}
public class MyClass {
private int a;
private void setA(int a) {
this.a = a;
}
}
With:
public class MyClass {
@PostConstruct
public void init() {
this.setA(1);
}
private int a;
private void setA(int a) {
this.a = a;
}
}
Is that correct? What are the differences between those options?