【问题标题】:Passing dynamic parameters to an annotation?将动态参数传递给注释?
【发布时间】:2012-09-24 15:42:26
【问题描述】:

我正在使用以下注释:

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

我想提取这些 IP 地址和端口中的每一个并将它们存储在文件 jmsendpoints.properties... 中,然后动态加载它们。像这样的:

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

有没有办法做到这一点?

【问题讨论】:

  • 您可以拥有动态解析的propertyValue = "jmsendpoints.properties ConnectionParameter"

标签: java annotations


【解决方案1】:

否。注释处理器(您正在使用的基于注释的框架)需要实现一种处理占位符的方法。


例如,Spring 中实现了类似的技术

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

这里Spring 实现了一种解析特定语法的方法,在这种情况下转换为类似于System.getProperty("dbName");

【讨论】:

    【解决方案2】:

    注解并非设计为在运行时可修改,但您可以利用诸如ASM 之类的字节码工程库来动态编辑注解值。

    相反,我建议您创建一个可以修改这些值的界面。

    public interface Configurable {
        public String getConnectionParameters();
    }
    
    public class TestMDB implements MessageDrivenBean, MessageListener, Configurable {
    
        public String getConnectionParameters() {
            return jmsEndpointsProperties.getConnectionParameters();
        }
    
        //...
    }
    

    您可能希望创建一个更面向键值的界面,但这是它的一般概念。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      相关资源
      最近更新 更多