method-specific decoration
with @Decorator.
By implementing the interface, you will have to implement all methods in @Decorator
(see Listing 15). The application
server is going to inject your
decorator instead of the real
class. To achieve that, you have
to introduce an interface shared
by both the decorator and the
Greeter bean. Now the interface is implemented by both
classes, GreetDecorator as well as
UniversalGreeter:
@Decorator
public abstract class GreetDecorator implements Greeter{
@Inject @Delegate
Greeter greeter;
@Override
public String greet() {
return "Good " + greeter.greet();
}
}
JAVA IN ACTION
Download all listings in this issue as text
GIVE BACK!
ADOPT A JSR
public class UniversalGreeter
implements Greeter {}
fashion all methods of a class and
introduce @Interceptors only if the
plumbing can be provably reduced.
The Greeter interface is injected
into the GreetingController instead
of the UniversalGreeter realization.
As in the interceptor case, you also
have to activate the decorators in
beans.xml first:
abstract decorators can be applied
on a subset of methods to cover
different aspects. Unlike interceptors, a decorator cannot be applied
with an @Stereotype, because it is
not sufficient for decoration.
Decorators require an interface,
which cannot be specified with
the @Stereotype annotation.
Because the vast majority of all
common cross-cutting concerns
come with the platform, rarely will
you find an obvious use case for AOP
and, thus, for interceptors or decorators in Java EE. </article>
Find your JSR here
ABOUT US
Back to the Real World
<beans>
<decorators>
<class>(...).aop.decorators.
GreetDecorator</class>
</decorators>
</beans>
All questions related to injection,
decoration, or interception in Java
EE 6 can usually be answered with
“yes.” Therefore, you should refine
such questions and ask whether it is
possible without custom extensions.
LEARN MORE
By making the decorator abstract,
you are not forced to implement all
methods of the Greeter interface.
Listing 16 shows partial decoration
with abstract decorators.
You can choose methods to decorate and ignore the rest. Multiple
Although it seems like everything
is possible in Java EE, you need only a
subset of the AOP capabilities introduced here in real-world projects. In
fact, I rarely use @InterceptorBinding
and mostly rely on the pure
@Interceptors annotation. I also tend
to intercept in an all-or-nothing
•;JSR 330 (Dependency Injection
for Java)
•;Seam Solder
•;CODI
blog
•;CanDI
•;Real;World;Java;EE;Night;Hacks—
Dissecting;the;Business;Tier
(press.adam-bien.com, 2011)