Java Reference
In-Depth Information
While XML has its problems, it can be beneficial in some ways. It can be easier to see how
the system components are organized by looking at a centralized XML configuration file.
You can also configure the same component differently per deployment or configure com-
ponents whose source code you can't change. Configuration that has little to do with Java
code is also poorly expressed in annotations. Examples of this include port/URL configura-
tion, file locations, and so on. The good news is that you can use XML with EJB 3. You can
even use XML to override or augment annotation-based configuration. Unless you have a
very strong preference for XML, it's generally advisable to start with annotations and use
XML overrides where they're really needed.
1.5.3. Intelligent defaults versus explicit configuration
EJB takes a different approach to default behavior than most frameworks such as Spring.
With Spring, for example, if you don't ask, you don't get. You have to ask for any behavior
you want to have in your Spring components. In addition to making the task of configura-
tion easier via annotations, EJB 3 reduces the total amount of configuration altogether by
using sensible defaults wherever possible. For example, the “Hello World” component is
automatically thread-safe, pooled, and transactional without you having to do anything at
all. Similarly, if you want scheduling, asynchronous processing, remoting, or web services,
all you need to do is add a few annotations to the component. There's no service that you'll
need to understand, explicitly enable, or configure—everything is enabled by default. The
same is true of JPA and CDI as well. Intelligent defaulting is especially important when
you're dealing with automated persistence using JPA.
1.5.4. Dependency injection versus JNDI lookup
EJB 3 was reengineered from the ground up for dependency injection. This means that you
can inject EJBs into other Java EE components and inject Java EE components into EJBs.
This is especially true when using CDI with EJB 3. For example, if you want to access the
HelloUser EJB in listing 1.1 from another EJB, Servlet, or JSF backing bean, you could
use code like this:
Search WWH ::




Custom Search