简体   繁体   English

在初始化SpringBootApplication之前寻找一种方法来解析springboot application.yml文件中的值

[英]Looking for a way to parse values in springboot application.yml file before SpringBootApplication is initialized

So my problem is as follows: 所以我的问题如下:

I'm using spring AMQP to connect to a rabbitMQ instance that using SSL. 我正在使用Spring AMQP连接到使用SSL的RabbitMQ实例。 Unfortunately, spring AMQP does not currently support full length amqps URIs and adding support is not high on the priority list (see issue: https://github.com/spring-projects/spring-boot/issues/6401 ). 不幸的是,spring AMQP当前不支持全长amqps URI,并且在优先级列表上添加支持的程度也不高(请参阅问题: https : //github.com/spring-projects/spring-boot/issues/6401 )。 They need to be separated. 他们需要分开。

The following fields are required in my application.yml to connect: 连接application.yml时需要以下字段:

spring:
  rabbitmq:
    host: hostname
    port: portnumber
    username: username
    password: password
    virtual-host: virtualhost
    ssl:
      enabled: true

My VCAP_Services environment for my rabbitMQ instance only provides the virtualhost and the full length uri in the following format: amqps://username:password@hostname:portnumber/virtualhost 我的RabbitMQ实例的VCAP_Services环境仅以以下格式提供虚拟主机和全长uri: amqps://username:password@hostname:portnumber/virtualhost

Copy and pasting these values into my application.yml is fine for now, but in the long run is not viable. 现在将这些值复制并粘贴到我的application.yml中是可以的,但从长远来看是不可行的。 They will need to come from vcap_services. 它们将需要来自vcap_services。

My @SpringBootApplication has @Beans that initialize a connection to the rabbitMQ instance on startup, so I am looking for a way to parse out the individual values and set them before the application is started. 我的@SpringBootApplication具有@Beans,这些@Beans在启动时会初始化与rabbitMQ实例的连接,因此我正在寻找一种方法来解析各个值并在应用程序启动之前进行设置。

Simply override Boot's auto configured connection factory... 只需覆盖Boot的自动配置的连接工厂...

@SpringBootApplication
public class So46937522Application {

    public static void main(String[] args) {
        SpringApplication.run(So46937522Application.class, args);
    }

    @Bean
    public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties config)
            throws Exception {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.getRabbitConnectionFactory()
            .setUri("amqps://guest:guest@10.0.0.3:5671/virtualhost");
        return connectionFactory;
    }

    @RabbitListener(queues = "si.test.queue")
    public void listen(Message in) {
        System.out.println(in);
    }

}

If you are just interested in reading the properties before your Spring Boot application is initialized, you can parse the yaml file using Spring's YamlPropertiesFactoryBean before you call SpringApplication.run . 如果您只想在初始化Spring Boot应用程序之前读取属性,则可以在调用SpringApplication.run之前使用Spring的YamlPropertiesFactoryBean解析yaml文件。 For example, 例如,

@SpringBootApplication
public class Application {

    public static void main(String[] args) {       
        YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
        yamlFactory.setResources(new ClassPathResource("application.yml"));
        Properties props = yamlFactory.getObject();

        String hostname = props.getProperty("spring.rabbitmq.hostname");
        ...

        SpringApplication.run(Application.class, args);
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM