当前位置:网站首页>Internship: compilation of other configuration classes
Internship: compilation of other configuration classes
2022-07-27 15:07:00 【ahyo】
The overall project architecture is better than that written before demo There are other configurations , For example, it involves hardware , You need to write code about hardware ; When it comes to remote sensing to obtain geographic information, we need to compile and quote Gaode api To achieve ; In addition, there are other piecemeal functions that need to be written , Make the whole project system more complete . But the only constant is controller service And its corresponding implementation class Then it corresponds to another service layer Write corresponding mapper, The first thing is to make a framework .
RedisConfig:
/** * redis To configure @EnableCaching @Configuration public class RedisConfig extends CachingConfigurerSupport { /** * Generate key The strategy of */
@Bean
@Override
public KeyGenerator keyGenerator() {
return (target, method, params) -> {
StringBuilder sb = new StringBuilder();
sb.append(method.getName());
for (Object obj : params) {
sb.append("_").append(obj.toString());
}
return sb.toString();
};
}
/** * Cache manager */
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = jackson2JsonRedisSerializer();
RedisCacheConfiguration redisCacheConfiguration =
RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.entryTtl(Duration.ofHours(1));
return RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}
/** * RedisTemplate To configure */
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = jackson2JsonRedisSerializer();
// serialize value Use this serialization method
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
// serialize key when
StringRedisSerializer srs = new StringRedisSerializer();
template.setKeySerializer(srs);
template.setHashKeySerializer(srs);
template.afterPropertiesSet();
return template;
}
public Jackson2JsonRedisSerializer jackson2JsonRedisSerializer() {
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
jackson2JsonRedisSerializer.setObjectMapper(new ObjectMapper()
.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY)
// solve jackson2 Cannot deserialize LocalDate、 LocalDateTime The problem of
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.registerModule(new JavaTimeModule())
// Set to store in cache according to specific type
.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
ObjectMapper.DefaultTyping.NON_FINAL,
JsonTypeInfo.As.WRAPPER_ARRAY));
return jackson2JsonRedisSerializer;
}
}
WebMvcConfigurer The configuration class is actually Spring An internal configuration , use JavaBean Instead of the traditional xml The configuration file is customized for the framework , You can customize some Handler,Interceptor,ViewResolver,MessageConverter. be based on java-based The way of spring mvc To configure , You need to create a configuration class and implement WebMvcConfigurer Interface ;
webconfig:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Value("${server.file-upload-path}")
private String filePath;
/** * * @param registry */
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedHeaders("*")
.allowedMethods("*")
.allowCredentials(true);
}
/** * Static path * @param registry */
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// By default "/**" Add on static file mapping "file: filePath/"
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/", "/","file:" + filePath);
}
/** * File upload temporary directory * @return */
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
String tmpPath = filePath + File.separator + "temp" + File.separator;
File file = new File(tmpPath);
if (!file.exists()) {
file.mkdirs();
}
factory.setLocation(tmpPath);
return factory.createMultipartConfig();
}
}
边栏推荐
- aac 和 h264等的时间戳
- NEFU119 组合素数【算术基本定理】
- [work] about technical architecture
- [medical industry] DICOM converter tools
- Confirm the time accuracy of the power supply setting voltage through the i/o function of vn1630/vn7640
- Summary of basic knowledge of C language
- Detailed explanation of Telnet remote login AAA mode [Huawei ENSP]
- @Bean 与 @Component 用在同一个类上,会发生什么?
- 网络设备硬核技术内幕 路由器篇 (10) CISCO ASR9900拆解 (四)
- LeetCode 240. 搜索二维矩阵 II medium
猜你喜欢

于不确定中见“安全感” 沃尔沃2022年中问道

软件产品第三方测试费用为什么没有统一的报价?

Stock trading 4

LeetCode 240. 搜索二维矩阵 II medium

LeetCode 781. 森林中的兔子 哈希表/数学问题 medium

面试官问:如何判断一个元素是否在可视区域?

Skywalking distributed system application performance monitoring tool - medium

Understand the evolution of redis architecture in one article

如何做好企业系统漏洞评估

视觉系统设计实例(halcon-winform)-9.文字显示
随机推荐
LeetCode 191. Number of 1 Bits(位1的个数) 位运算/easy
Differences among CPU, GPU and NPU
Nefu117 number of prime numbers [prime number theorem]
NEFU119 组合素数【算术基本定理】
视觉系统设计实例(halcon-winform)-10.PLC通讯
移动端使用vantUI的list组件,多个tab项来回切换时,列表加载多次导致数据无法正常展示
lc marathon 7.26
Forward proxy and reverse proxy
什么是Tor?Tor浏览器更新有什么用?
终于有人把面试必考的动态规划、链表、二叉树、字符串全部撸完了
What is the execution method of the stand-alone parallel query of PostgreSQL?
Research on Chinese idiom metaphorical knowledge recognition and relevance based on transfer learning and text enhancement
Getting started with DirectX
图解 SQL,这也太形象了吧
@Bean 与 @Component 用在同一个类上,会发生什么?
被动收入:回归原始且安全的两种赚取方法
The database uses PSQL and JDBC to connect remotely and disconnect automatically from time to time
对话框管理器第三章:创建控件
Skywalking distributed system application performance monitoring tool - medium
Lesson 3: SPFA seeking the shortest path