Is there a function which is called every time an EJB is looked up? I need to refresh some data before any method is called.
Not sure I get exactly what you mean. First you probably need to distinguish between stateless and stateful EJBs.
Stateless EJB are well, stateless, and should not contain data. The app. server can decide to destroy or reconstruct it anytime. You don't actually hold a reference to a particular instance which is created when it's looked up. The app. server maintain a pool of EJBs, and one of them is used per invocation.
Stateful EJB can contain data. You hold a reference to one specific instance, which is created when the bean is looked up. Callback methods can be specified with
@PostConstruct
or@PreDestroy
(These callbacks also exist for stateless EJBs but make less sense).
That said, if you need to perform something before a method is called, I suggest you use an interceptor (using @Interceptor
, works for stateless and stateful EJBs). Just like with AOP, you get a chance to perform something before and after the bean method is actually executed, e.g refresh the cache.