当前位置:网站首页>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();
}
}
边栏推荐
- Annual comprehensive analysis of China's online video market in 2022
- 什么是Tor?Tor浏览器更新有什么用?
- NEFU118 n! How many zeros are there after [basic theorem of arithmetic]
- Research on multi label patent classification based on pre training model
- NEFU118 n!后面有多少个0【算术基本定理】
- 多表查询_子查询概述和多表查询_子查询情况1&情况2&情况3
- 如何帮助企业优化Office管理
- LeetCode 191. Number of 1 Bits(位1的个数) 位运算/easy
- 网络设备硬核技术内幕 路由器篇 5 汤普金森漫游网络世界(上)
- TXT把换行 替换为空格或者取消换行
猜你喜欢

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

Getting started with DirectX

What you want most is the most comprehensive summary of C language knowledge. Don't hurry to learn

LeetCode 783. 二叉搜索树节点最小距离 树/easy

谷歌团队推出新Transformer,优化全景分割方案|CVPR 2022

股票买卖4

Redis

LeetCode 240. 搜索二维矩阵 II medium

If we were the developer responsible for repairing the collapse of station B that night
![Detailed explanation of Telnet remote login AAA mode [Huawei ENSP]](/img/48/dd0fa3c494c45f604b7a93c707b808.png)
Detailed explanation of Telnet remote login AAA mode [Huawei ENSP]
随机推荐
SkyWalking分布式系统应用程序性能监控工具-中
股票买卖4
网络设备硬核技术内幕 路由器篇 21 可重构的路由器
Web page table table, realizing rapid filtering
Is there a regular and safe account opening platform for gold speculation
Lecture 4: Longest ascending substring
Web页面table表格,实现快速筛选
Understand the evolution of redis architecture in one article
LeetCode 781. 森林中的兔子 哈希表/数学问题 medium
User question understanding and answer content organization for epidemic disease Science Popularization
代码覆盖率统计神器-jacoco工具实战
MySQL save data prompt: out of range value for column error
被动收入:回归原始且安全的两种赚取方法
如何做好企业系统漏洞评估
Automatically configure SSH password free login and cancel SSH password free configuration script
Stock trading 4
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
数据仓库项目从来不是技术项目
Nokia's patent business was hit for the first time, and Chinese enterprises are not so easy to knead
lc marathon 7.26