mercredi 8 avril 2015

Can @PostConstruct be used as a substitute of @Bean/@Produces?

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?


Aucun commentaire:

Enregistrer un commentaire