end-user end-user - 1 year ago 135
Java Question

How can I use a constant to enter multiple @CrossOrigins?

I have a

annotation on several of my controllers. I'd like to be able to set a
static final
somewhere and have all of them refer to it. Something like:

public static final String[] ORIGINS = {"domain1","domain2"};
@CrossOrigin(origins = ORIGINS)

However, I clearly have some syntax off.

For those who are interested in doing the same thing, here's what I did:

@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
@CrossOrigin(origins = {
@interface CORSPermittedController {

That allows me to annotate all my controllers with

Answer Source

Unfortunately, you can't do that with Java syntax. Attribute values must be compile constants (primitive, String literals, or arrays of these types).

But you could write a bean post processor which is responsible for putting this annotation over a class at runtime by using javassist.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download