简体   繁体   English

寻找一种在应用程序运行时覆盖 application.properties 值的方法

[英]Looking for a way to override application.properties values during application runtime

I have written a small application to publish messages to IBM MQ using Spring boot.I have following properties in my applications.properties.我编写了一个小型应用程序,使用 Spring boot 将消息发布到 IBM MQ。我的 applications.properties 中有以下属性。 Code is working when I input a destination queue name and the message without any issue, Now I want to input the queue manager name and based on the queue manager name ibm.mq.QueueManager,ibm.mq.connName should be changed.当我输入目标队列名称和消息时代码正常工作,没有任何问题,现在我想输入队列管理器名称并根据队列管理器名称 ibm.mq.QueueManager,ibm.mq.connName 应该更改。 Appreciate if someone give me a suggestion.感谢有人给我一个建议。 Do I need to come up with a separate method with a MQ connection initiation?我需要想出一个单独的方法来启动 MQ 连接吗?

ibm.mq.QueueManager=KAU.TST
ibm.mq.channel=KAU.CONN
ibm.mq.connName=192.168.1.26(1540)
ibm.mq.user=
ibm.mq.password=

I have included following setter in my controller class, but it seems it does not overriding the property file as I intended.我在我的 controller class 中包含了以下设置器,但它似乎没有按照我的意图覆盖属性文件。

    @Value("${ibm.mq.QueueManager:KAU.TST}")
    String destQM;


    public void setQueueManager(String destQM) {
        this.destQM= destQM;
    }

current method of publishing messages当前发布消息的方法

    @Autowired
    private JmsTemplate jmsTemplate;
    public String dropmessage()
    {
        
          try{
              //String msg="IBM MQ integration testing with spring boot";
              jmsTemplate.convertAndSend(qname, msg);
              System.out.println("Message Sent :"+msg);
              return "OK";
          }catch(JmsException ex){
              ex.printStackTrace();
              return "FAIL";
          }     
        
    }

You can use factory customizer methods to make dynamic changes to qmgr configurations.您可以使用工厂定制器方法对 qmgr 配置进行动态更改。 For example:例如:

@Bean
public MQConnectionFactoryCustomizer myCustomizer() {
  MQConnectionFactoryCustomizer c = new MQConnectionFactoryCustomizer() {
  @Override
  public void customize(MQConnectionFactory factory) {
    System.out.println("Changing port for " + factory.getQueueManager());
     try {
       factory.setPort(1415);
     } catch (JMSException e) { 
     }
   }
 };
 return c;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mvn test - 覆盖 application.properties 中的值 - mvn test - override values in application.properties 如何在 Spring-Boot 的生产过程中覆盖 application.properties? - How to override application.properties during production in Spring-Boot? 覆盖application.properties中的值 - Override value in application.properties 如何在运行时编辑application.properties(供下次使用) - How to edit application.properties during runtime (for next time use) 在运行时设置application.properties - Set application.properties at runtime 从命令行覆盖application.properties文件中的值 - override values in application.properties files from command line Spring如何在运行时从application.properties重新加载值 - Spring how to reload the values from application.properties at runtime 如何加载外部属性文件并覆盖springboot application.properties(没有运行时参数)? - How to load external properties file and override springboot application.properties (without runtime args)? 子应用程序中的application.properties文件不会覆盖主应用程序中的application.properties文件 - application.properties file from child application not override application.properties file in main application 用application.properties覆盖默认的Spring-Boot application.properties - Override default Spring-Boot application.properties with application.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM