Java Reference
In-Depth Information
@Decorator
public abstract class CoderDecorator implements Coder {
@Inject
@Delegate
@Any
Coder coder;
@Override
public String codeString(String s, int tval) {
int len = s.length();
return "\"" + s + "\" becomes " + "\"" + coder.codeString(s,
tval)
+ "\", " + len + " characters in length";
}
}
The decorator's
codeString
method calls the delegate object's
codeString
method
to perform the actual encoding.
The
decorators
example includes the
Logged
interceptor binding and
LoggedIn-
terceptor
class from the
billpayment
example. For this example, the interceptor is
set on the
CoderBean.encodeString
method and the
CoderImpl.codeString
method. The interceptor code is unchanged; interceptors are usually reusable for different
applications.
Except for the interceptor annotations, the
CoderBean
and
CoderImpl
classes are
identical to the versions in the
encoder
example.
The
beans.xml
file specifies both the decorator and the interceptor:
<decorators>
<class>decorators.CoderDecorator</class>
</decorators>
<interceptors>
<class>decorators.LoggedInterceptor</class>
</interceptors>
Running the
decorators
Example
You can use either NetBeans IDE or Ant to build, package, deploy, and run the
decor-
ators
application.