以 RabbitMQ 为例,yml 文件配置如下:

    1. application:
    2. rabbit:
    3. default-message:
    4. exchange:
    5. name: defaultExchange
    6. queue:
    7. name: defaultQueue
    8. binding:
    9. routing-key: #

    代码中通过构造器初始化后,可以不配置 yml 。配置了 yml 会覆盖构造器中配置。

    1. @Getter
    2. @Configuration
    3. @ConfigurationProperties(prefix = "application.rabbit", ignoreUnknownFields = false)
    4. public class ApplicationRabbitMQProperties {
    5. /**
    6. * 死信交换机默认配置
    7. */
    8. private final DefaultMessage defaultMessage = new DefaultMessage();
    9. @Getter
    10. public static class DefaultMessage {
    11. private final CustomerExchange exchange = new CustomerExchange();
    12. private final CustomerQueue queue = new CustomerQueue();
    13. private final CustomerBinding binding = new CustomerBinding();
    14. }
    15. @Data
    16. public static class CustomerExchange {
    17. /**
    18. * 交换机名称
    19. */
    20. private String name = "defaultExchange";
    21. /**
    22. * 交换机是否持久化
    23. */
    24. private Boolean durable = true;
    25. /**
    26. * 是否自动删除空闲的交换机
    27. */
    28. private Boolean autoDelete = false;
    29. /**
    30. * 对交换机属性进行配置
    31. */
    32. private Map<String, Object> arguments = new HashMap<>(10);
    33. }
    34. @Data
    35. public static class CustomerQueue {
    36. /**
    37. * 队列名称
    38. */
    39. private String name = "defaultQueue";
    40. /**
    41. * 队列是否持久化
    42. */
    43. private Boolean durable = true;
    44. /**
    45. * 是否连接独占
    46. */
    47. private Boolean exclusive = false;
    48. /**
    49. * 是否自动删除空闲的交换机
    50. */
    51. private Boolean autoDelete = false;
    52. /**
    53. * 对消息属性进行配置
    54. */
    55. private Map<String, Object> arguments = new HashMap<>(10);
    56. }
    57. @Data
    58. public static class CustomerBinding {
    59. /**
    60. * 默认路由(#:表示拦截任意字符的路由,*:表示拦截任意的单个字符,其他拦截比如:user.ABC 等自定义路由)
    61. */
    62. private String routingKey = "#";
    63. }
    64. }