hello,大家好,有今天没有写文章了,因为呢,最近,公司有点事情,需要我呢,搭建一个demo,框架呢是springboot+Active实现消息的延时发送和定时发送功能,为了项目以后中使用到,所以呢,看springboot+ActiveMq 能不能实现这个场景,接下来呢,我说一下我搭好框架之后,遇到的一个问题吧。

如何获取application.yml文件数据,或者application.properties文件值

我的是application.yml

activemq:
 brokerUrl: tcp://127.0.0.1:61616
 user: admin
 password: admin
 #in-memory: true #是否启用内存模式(也就是不安装MQ,项目启动时同时也启动一个MQ实例)
 # 等待消息发送响应的时间。设置为0等待永远。 send-timeout
 timeout: 0
 # 队列名称
 queueName: springbootActiveMqTest
 topicName: publishTopic
 # 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。
 non-blocking-redelivery: false
 pool:
 enabled: true
 max-connections: 10

第一种方式:

1.定义一个实体类,然后扫描到

@Component
@ConfigurationProperties(prefix = "activemq")
public class MyProps {
 //用户名
 private String user;
 //密码
 private String password;
 //链接地址
 private String brokerUrl;
 // 超时时间
 private String timeout;
 // 队列名称
 private String queueName;

然后实现set ,get 方法。

测试类:2

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AdminApplication.class)
public class TestActiveMq {
//第一种方式获取
 @Autowired
 private MyProps myProps;
//第二种方式获取
 @Value("${user.userName}")
 private String userName;
 @Test
 public void test_send() {
 logger.info("发送开始测试-------------------------------------------------------");
 logger.info("用户名3------------------------------"+myProps.getUser());
 }
}
 @Test
 public void test_send() {
 logger.info("发送开始测试-------------------------------------------------------");
 logger.info("用户名2------------------------------"+userName);
 }

结果也没获取到,你们相信吗,这居然被我遇到了,因为这种几种方式获取呢,以前写过,也没出现过这种问题,我靠,脑袋有点懵了

于是第三种方式来了

YamlPropertiesFactoryBean yamlMapFactoryBean = new YamlPropertiesFactoryBean();
 //可以加载多个yml文件
 yamlMapFactoryBean.setResources(new ClassPathResource("application.yml"));
 Properties properties = yamlMapFactoryBean.getObject();
 userCommon.setUser(properties.getProperty("activemq.user"));
 userCommon.setPassword(properties.getProperty("activemq.password"));
 retrun userCommon;

我靠,居然获取到了,你说气人不,

// 测试类pom.xml 配置
 junit
 junit
 4.12
// 扫描文件配置
 org.springframework.boot
 spring-boot-configuration-processor
 true
// test 配置
 org.springframework.boot
 spring-boot-starter-test
 test

还有更奇葩的是;

在测试类上面的这个扫描

@RunWith(SpringRunner.class)

@SpringBootTest(classes = AdminApplication.class)

死活没法引用,查了很多方法,虽然也有同志遇到过这个问题,但答案很多,idea 始终无法引用

换成eclipse 引入这个项目,然后,随便敲一下,就可以引入了,我靠,然后在idea 这边居然还是报红,我靠, 我没救了,

最后,写了个main 方法实现这个Active MQ 调用生产者发送消息吧,然后消费者也是这样的main 方法消费的,好吧,至少实现了这个 Active mq 消息延时发放和定时发送的功能了 ,

先这样吧,今天有点无语了,

特别是这个引入的问题,大家觉得,这个简单的问题,会是哪个细节出了问题呢。欢迎大家留言,讨论,谢谢。