• 使用@ContextConfiguration或者@ContextWebConfiguration注解调用resource文件夹下面的yml文件


    在测试用例中,指定初始化方式 @ContextConfiguration(classes = RedisConf.class, initializers = ConfigFileApplicationContextInitializer.class)

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = RedisConf.class, initializers = ConfigFileApplicationContextInitializer.class)
    public class RedisTest {
        @Test
        public void testRedis() {
            String ans = JedisClient.getStr("hello");
            System.out.println(ans);
        }
    }

    对应的配置类

    @Configuration
    @PropertySource(value = "classpath:application.yml")
    public class RedisConf {
    
        @Autowired
        private Environment environment;
    
        @Autowired
        public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(redisConnectionFactory);
    
            DefaultStrSerializer serializer = new DefaultStrSerializer();
            redisTemplate.setValueSerializer(serializer);
            redisTemplate.setHashValueSerializer(serializer);
            redisTemplate.setKeySerializer(serializer);
            redisTemplate.setHashKeySerializer(serializer);
    
            redisTemplate.afterPropertiesSet();
    
            JedisClient.register(redisTemplate);
            return redisTemplate;
        }
    
        @Bean
        public RedisConnectionFactory redisConnectionFactory() {
            LettuceConnectionFactory fac = new LettuceConnectionFactory();
            fac.getStandaloneConfiguration().setHostName(environment.getProperty("spring.redis.host"));
            fac.getStandaloneConfiguration().setPort(Integer.parseInt(environment.getProperty("spring.redis.port")));
            fac.getStandaloneConfiguration()
                    .setPassword(RedisPassword.of(environment.getProperty("spring.redis.password")));
            fac.afterPropertiesSet();
            return fac;
        }
    }

    本文转自:https://www.cnblogs.com/yihuihui/p/9174722.html

  • 相关阅读:
    从国内流程管理软件市场份额看中国BPM行业发展
    为什么流程是成功企业的关键?
    港真,到底应该选择OA还是BPM?
    hello world
    i40e网卡驱动遇到的一个问题
    suse 11 sp3编译报错问题
    linux scsi相关的一些学习笔记
    linux tcp 在timewait 状态下的报文处理
    趋势科技 redirfs模块的一个小问题
    linux 3.10 一个扇区异常可能引发的hung
  • 原文地址:https://www.cnblogs.com/nizuimeiabc1/p/12153270.html
Copyright © 2020-2023  润新知