Backing Bean Constructur Called Multiple Times

If the constructor of a backing bean is called multiple times during one single request, the reason might be that the backing bean has no scope annotation. I experienced this problem using CDI, but it might also be the case for JSF managed beans. I expected the default scope to be @RequestScoped, but as it turned out this is not the case. For CDI the default scope is @Dependent and for JSF it’s None – which seems to be basically the same:

Before using @RequestScoped for my backing bean, the following actions took place when calling a simple registration backing bean:

After the first request of a simple registration form:
 ...
constructor called
getUsername()
 ...
After filling out the fields username, password
passwordAgain and clicking the register button:
 ...
constructor called
getUsername()
constructor called
constructor called
getUsername()
constructor called
constructor called
getPassword()
constructor called
constructor called
getPasswordAgain()
constructor called
setUsername(Homer)
constructor called
setPassword(foobar)
constructor called
setPasswordAgain(foobar)
constructor called

Not only was the constructor called way to often, it was also called one more time after all fields had been set. This means that no fields made it to the database, because they were all empty. It took me hours just to realize that this was no persistence or hibernate problem… so keep this nasty default scope in mind and use some of the explicit scope annotations such as @RequestScoped.