1

I need to load init parameters from an external properties files through a service i already have. this part is Tested , but i'm struggling to inject my init params on application start and make them accessible to the whole application. should i use an applicationScoped managed bean ?

thanks in advance

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Genjuro
  • 7,405
  • 7
  • 41
  • 61

2 Answers2

2

Create an EJB Startup Service

@Startup

The introduction of singletons also provides a convenient way for EJB applications to receive callbacks during application initialization or shutdown. By default, the container decides when to instantiate the singleton instance. However, you can force the container to instantiate the singleton instance during application initialization by using the @Startup annotation. This allows the bean to define a @PostConstruct method that is guaranteed to be called at startup time. In addition, any @PreDestroy method for a singleton is guaranteed to be called when the application is shutting down, regardless of whether the singleton was instantiated using lazy instantiation or eager instantiation. In lazy instantiation, the singleton isn't instantiated until it's method's are first needed. In eager instantiation, the singleton is instantiated at startup time whether or not it gets used.

@Singleton
@Startup
public class StartupBean {

@PostConstruct
private void startup() { ... }

@PreDestroy
private void shutdown() { ... }
...
}

If you are in a clustered environment this probably will not work for you out of the box. This only works within a single VM. You will have your StartupBean called on every server instance starting up or shutting down. If you have special cluster requirements (e.g. only initialize once for the complete cluster) you have to think about synchronizing your StartupBeans by using a database. Usable on any EJB 3.1 compliant container and highly portable. This even should work with the lightweight Java EE 6 Webprofile

https://blogs.oracle.com/enterprisetechtips/entry/a_sampling_of_ejb_3

Rotka
  • 430
  • 2
  • 11
1

IF you want to work with ManagedBean

Eager Application-Scoped Beans Managed beans are lazily instantiated. That is, that they are instantiated when a request is made from the application.

To force an application-scoped bean to be instantiated and placed in the application scope as soon as the application is started and before any request is made, the eager attribute of the managed bean should be set to true as shown in the following example:

@ManagedBean(eager=true)
@ApplicationScoped

http://docs.oracle.com/javaee/6/tutorial/doc/girch.html

Rotka
  • 430
  • 2
  • 11