Virginie
Virginie

Reputation: 949

MyFaces CODI instantiates beans twice

I'm migrating a JSF 2.1 application to CDI. To be able to use the @javax.faces.bean.ViewScoped annotation, I'm trying to use MyFaces CODI, as suggested here.

The application seems to be working as expected, but I noticed by logging that the constructor of each of my @Named beans is called twice when I first access it. However, their init() methods annotated with @PostConstruct are only executed once.

When those beans are used for the second or nth time, their constructor is only called once as normally expected.

It doesn't happen as soon as I remove the MyFaces CODI jars from my app, so I'm sure it is caused by it.

Is this a normal behaviour, and if so, why? Does it have an impact on performance or could this cause any problem?

I'm feeling this could be linked with a proxy creation, but it's not 100% clear in my mind so I would greatly appreciate an explanation.

My configuration:

Thanks.

Upvotes: 0

Views: 244

Answers (2)

Dar Whi
Dar Whi

Reputation: 822

@rdcrng is correct.

With CDI you need @PostConstruct methods instead.

Upvotes: 0

rdcrng
rdcrng

Reputation: 3443

Yes, it has everything to do with proxy creation and it is the expected behavior with CDI. See this https://community.jboss.org/blogs/stuartdouglas/2010/10/12/weld-cdi-and-proxies, as you could probably find other explanations that say the same thing.

But as to why it happens with @javax.faces.bean.ViewScoped and CODI, that's because CODI activates a CDI extension, that scans for beans with the JSF ViewScope and replaces it at runtime with their own CDI based implementation.

Upvotes: 3

Related Questions