{8.1.3-new} Dynamically Applied Annotations

In the section {@doc juneau-marshall.Transforms}, you were introduced to annotations that can be applied to bean classes, methods, fields, and constructors such as {@link oaj.annotation.Bean @Bean}:

// Address class with only street/city/state properties (in that order). // All other properties are ignored. @Bean(bpi="street,city,state") public class Address { ... }

An alternate way of applying these annotations is using the @XConfig annotations introduced in section {@doc juneau-marshall.ConfigurableAnnotations}.

// Unannotated class. public class Address { ... } @BeanConfig( applyBean={ @Bean(on="Address", bpi="AddressBean: street,city,state") } } public static class DummyClass {} WriterSerializer ws = JsonSerializer.create().applyAnnotations(DummyClass.class).build(); String json = ws.toString(addressBean);

The advantage to this approach is it allows you to use Juneau annotations on classes/methods/fields/constructors where you might not have access to the source code, or when you only want to selectively apply the annotation under certain scenarios instead of globally.

For example, the following shows the @Bean annotation being selectively applied on a single REST method (described later in {@doc juneau-rest-server}):

@RestMethod @BeanConfig( applyBean={ @Bean(on="Address", bpi="AddressBean: street,city,state") } } public List<Address> getAddresses() {}

Any Juneau annotation that has an on() method can be applied dynamically this way. These include:

The valid pattern matches are: