javajsfmanaged-beanfaces-config

Create managed bean application scoped to load on the app start-up


I'm new to Java EE and I have a task to do but no idea how to do it. I need to create a manged bean that will be scoped on the application. Everytime we start the application, the bean needs to load a list of data from database. So, according to my research on the web, I need:

  1. Create a managed Bean.
  2. Add the bean name to faces-config as an application scoped bean.
  3. On the bean, add all the methods to load the datas.

So how to set loading at the application start-up ? And then how to get these loaded datas from anywhere in the app?


Solution

  • This is quite easy since JSF 2.x, just add attribute eager to the @ManagedBean annotation.

    @ApplicationScoped
    @ManagedBean(eager=true)
    public class InitializerBean {
    
        @PostConstruct
        public void init() {
             //init your DB here
        }
    
    }