concept lookup in category ejb

appears as: lookup, lookups, lookup
EJB 3 in Action, Second Edition

This is an excerpt from Manning's book EJB 3 in Action, Second Edition.

Table 1.1. EJB services

Service

What it means for you

Registry, dependency injection, and lookup Helps locate and glue together components, ideally through simple configuration. Lets you change component wiring for testing.
Lifecycle management Lets you take appropriate actions when the lifecycle of a component changes, such as when it’s created and when it’s destroyed.
Thread safety EJB makes all components thread-safe and highly performant in ways that are completely invisible to you. This means that you can write your multithreaded server components as if you were developing a single-threaded desktop application. It doesn’t matter how complex the component is; EJB will make sure it’s thread-safe.
Transactions EJB automatically makes all of your components transactional, which means you don’t have to write any transaction code while using databases or messaging servers via JDBC, JPA, or Java Message Service (JMS).
Pooling EJB creates a pool of component instances that are shared by clients. At any point in time, each pooled instance can only be used by a single client. As soon as an instance is done servicing a client, it’s returned to the pool for reuse instead of being frivolously discarded for the garbage collector to reclaim.
You can also specify the size of the pool so that when the pool is full, any additional requests are automatically queued. This means that your system will never become unresponsive trying to handle a sudden burst of requests.
Similar to instance pooling, EJB also automatically pools threads across the container for better performance.
State management The EJB container manages the state transparently for stateful components instead of having you write verbose and error-prone code for state management. This means that you can maintain the state in instance variables as if you were developing a desktop application. EJB takes care of all the details of session/state maintenance behind the scenes.
Memory management EJB steps in to optimize memory by saving less frequently used stateful components into the hard disk to free up memory. This is called passivation. When memory becomes available again and a passivated component is needed, EJB puts the component back into memory. This is called activation.
Messaging EJB 3 allows you to write message processing components without having to deal with a lot of the mechanical details of the JMS API.
Security EJB allows you to easily secure your components through simple configuration.
Scheduling EJB lets you schedule any EJB method to be invoked automatically based on simple repeating timers or cron expressions.
Asynchronous processing You can configure any EJB method to be invoked asynchronously if needed.
Interceptors EJB 3 introduces AOP (aspect-oriented programming) in a lightweight, intuitive way using interceptors. This allows you to easily separate out crosscutting concerns such as logging and auditing, and to do so in a configurable way.
Web services EJB 3 can transparently turn business components into Simple Object Access Protocol (SOAP) or Representational State Transfer (REST) web services with minimal or no code changes.
Remoting In EJB 3, you can make components remotely accessible without writing any code. In addition, EJB 3 enables client code to access remote components as if they were local components using dependency injection (DI).
Testing You can easily unit- and integration-test any EJB component using embedded containers with frameworks like JUnit.

After you’ve connected to your JNDI provider, the Context interface provides the functionality to interact with the provider and get resources. For this primer, we’ll concentrate on the lookup method and leave the rest for you to explore on your own. Table 5.4 describes this method.

Injection using lookup

Using the lookup attribute takes all the guesswork out of EJB injection. Instead of the container trying to resolve the correct bean, you can tell the container exactly which bean you want by specifying the actual JNDI name:

@EJB(lookup="java:module/DefaultAccountService")
AccountServices accountServices;

In this example, the container detects the @EJB annotation and the value for lookup narrows down what to look up from JNDI and inject. You can use any naming scope that makes sense.