How can they be initialized in the constructor then, just a little confusion? (I know that the constructor is called before the method annotated by @PostConstruct is invoked).
Use the @PostConstruct
's init
method only to initialize fields which are being injected (i.e Ejbs). If you don't have any injections and dependencies, init
method becomes pretty useless. The bean's constructor is used to initialize bean's own properties. In your example, you don't have any injection, so you can safely remove that @PostConstruct
's init
method.
If these fields are initialized in the method annotated by @PostConstruct i.e init(), in this case (instead of initializing them in the constructor as shown in the snippet), their specified values are not set unless and until a user logs in (unless a session is created).
It's the normal behavior, there is no reason to emphasize on the not, because a @SessionScoped
bean is created and initialized only when a new session is created.
For more info check the link, the question has already been answered : Why use @PostConstruct?