天天看點

springboot多子產品項目子子產品加載application.properties

springboot多子產品項目會将一些通用的功能提取出來作為一個子子產品。

不是多子產品項目時,我們在application.properties中配置資訊會在項目啟動時自動加載配置,能夠自動加載配置的原因是啟動類注解@SpringBootApplication 是多個注解的集合,而其中一個注解@EnableAutoConfiguration注解 能夠自動加載application.properties資訊去配置(可參考文章:https://blog.csdn.net/zxc123e/article/details/80222967)

springboot 多子產品項目的子子產品如果不直接通過啟動類運作而僅僅隻是其他子產品的一個依賴,那麼這個子子產品是不會自動加載自己的application.properties配置的。

子子產品中 用@PropertySource(“classpath:application.properties”)注解 主動去加載application.properties 也不能成功加載配置子子產品自己的application.properties。

如下:

@Configuration
@PropertySource("classpath:application.properties")
public class RedisConfig {
    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}
           

上述@PropertySource(“classpath:application.properties”) 不會加載子子產品自己的application.properties

解決辦法,将子子產品 application.properties 改名後,@PropertySource就能夠成功加載配置了!

将子子產品的application.properties 改名為 redis.properties 後

@Configuration
@PropertySource("classpath:redis.properties")
public class RedisConfig {
    @Bean
    @SuppressWarnings("all")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}
           

子子產品中配置 redis.properties 會在其他有啟動類子產品啟動後自動加載。

參考來源:https://stackoverflow.com/questions/46784051/spring-boot-multi-module-project-load-property-file

繼續閱讀