Springbootマルチモジュールプロジェクトサブモジュールロードアプリケーション.properties
12645 ワード
Springbootマルチモジュールプロジェクトでは、一般的なdaoモジュール、redisモジュールなどの一般的な機能をサブモジュールとして抽出します.マルチモジュールプロジェクトではない場合、アプリケーションにいます.propertiesの構成情報は、プロジェクトの開始時に自動的に構成をロードします.構成を自動的にロードできるのは、起動クラス注釈@SpringBootApplicationが複数の注釈の集合であるのに対し、そのうちの1つの注釈@EnableAutoConfiguration注釈が自動的にアプリケーションをロードできるためです.properties情報の構成(記事参照:https://blog.csdn.net/zxc123e/article/details/80222967)
Springbootマルチモジュールプロジェクトのサブモジュールが起動クラスを直接通過することなく他のモジュールの1つの依存にすぎない場合、このサブモジュールは自動的に自分のアプリケーションをロードしない.propertiesで構成されています.
サブモジュールでは@PropertySource("classpath:application.properties")注釈でアプリケーションをアクティブにロードする.propertiesも構成サブモジュール自体のアプリケーションを正常にロードできない.properties. 次のようになります.
上記@PropertySource("classpath:アプリケーション.properties")では、サブモジュール自体のアプリケーションはロードされません.properties
解決策は、サブモジュールをアプリケーションします.propertiesが名前を変更すると、@PropertySourceは構成を正常にロードできます!
サブモジュールのアプリケーションをpropertiesはredisと名前を変えた.properties後
サブモジュールにredisを構成する.propertiesは、他の起動クラスモジュールが起動した後に自動的にロードされます.参照元:https://stackoverflow.com/questions/46784051/spring-boot-multi-module-project-load-property-file
Springbootマルチモジュールプロジェクトのサブモジュールが起動クラスを直接通過することなく他のモジュールの1つの依存にすぎない場合、このサブモジュールは自動的に自分のアプリケーションをロードしない.propertiesで構成されています.
サブモジュールでは@PropertySource("classpath:application.properties")注釈でアプリケーションをアクティブにロードする.propertiesも構成サブモジュール自体のアプリケーションを正常にロードできない.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:アプリケーション.properties")では、サブモジュール自体のアプリケーションはロードされません.properties
解決策は、サブモジュールをアプリケーションします.propertiesが名前を変更すると、@PropertySourceは構成を正常にロードできます!
サブモジュールのアプリケーションを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