Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing dynamic parameters to an annotation?

I am using the following annotation:

@ActivationConfigProperty(
    propertyName = "connectionParameters", 
    propertyValue = "host=127.0.0.1;port=5445,host=127.0.0.1;port=6600"),
public class TestMDB implements MessageDrivenBean, MessageListener

I would like to pull each of these IP addresses and ports and store them in a file jmsendpoints.properties... then load them dynamically. Something like this:

@ActivationConfigProperty(
    propertyName = "connectionParameters", 
    propertyValue = jmsEndpointsProperties.getConnectionParameters()),
public class TestMDB implements MessageDrivenBean, MessageListener

Is there a way to do that?

like image 339
Nicholas DiPiazza Avatar asked Sep 24 '12 15:09

Nicholas DiPiazza


2 Answers

No. The annotation processor (the annotation-based framework you're using) needs to implement an approach to handling placeholders.


As an example, a similar technique is implemented in Spring

@Value("#{systemProperties.dbName}")

Here Spring implements an approach to parsing that particular syntax, which in this case translates to something similar to System.getProperty("dbName");

like image 191
Johan Sjöberg Avatar answered Nov 04 '22 19:11

Johan Sjöberg


Annotations are not designed to be modifiable at runtime, but you might be able to utilize a bytecode engineering library such as ASM in order to edit the annotation values dynamically.

Instead, I recommend creating an interface in which you could modify these values.

public interface Configurable {
    public String getConnectionParameters();
}

public class TestMDB implements MessageDrivenBean, MessageListener, Configurable {

    public String getConnectionParameters() {
        return jmsEndpointsProperties.getConnectionParameters();
    }

    //...
}

You might wish to create a more key-value oriented interface, but that's the general concept of it.

like image 31
FThompson Avatar answered Nov 04 '22 17:11

FThompson